Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9029687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:18:21+00:00 2026-06-16T07:18:21+00:00

I was able to get my method (below) to run in LinqPad, but when

  • 0

I was able to get my method (below) to run in LinqPad, but when switching to my actual code (using Entity Framework), I get this error:

“Unable to cast the type ‘System.Nullable`1’ to type ‘System.Object’.
LINQ to Entities only supports casting EDM primitive or enumeration
types.”

The error occurs if I uncomment either of the two commented lines here (the actual error doesn’t occur until the last line in the method is run):

public List<LotEquipmentScan> GetLotEquipmentScans(string maximoAssetNumber, decimal? manufacturerId, decimal? partNumber)
{
    var predicate = PredicateBuilder.True<LotEquipmentScanRecord>();

    // Note: Can't use == in where clause because of the possibility of nulls. That's why we're using object.Equals().
    if (maximoAssetNumber != "x") { predicate = predicate.And(scan => object.Equals(scan.MaximoAssetNumber, maximoAssetNumber)); }
    //if (manufacturerId != -1) { predicate = predicate.And(scan => object.Equals(scan.RawMaterialLabel.ManufacturerId, manufacturerId)); }
    //if (partNumber != -1) { predicate = predicate.And(scan => object.Equals(scan.RawMaterialLabel.PartNumber, partNumber)); }

    return Db.LotEquipmentScanRecords.AsExpandable().Where(predicate).ToList().Map();
}

I believe this is happening because manufacturerId and partNumber are nullable decimals. The issue is that those variables can be null, and we even want to filter the results by them being null. Does this just not work with EF, or is there an elegant way around this?

EDIT

To be clear, when manufacturerId is passed in as null, using this line returns five rows (which I can verify by looking at the DB):

if (manufacturerId != -1) { predicate = predicate.And(scan => object.Equals(scan.RawMaterialLabel.ManufacturerId, manufacturerId)); }

Using this line, no rows are returned:

if (manufacturerId != -1) { predicate = predicate.And(scan => scan.RawMaterialLabel.ManufacturerId == manufacturerId); }

The problem is when I pass in a good manufacturerId, then I get the error above.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T07:18:22+00:00Added an answer on June 16, 2026 at 7:18 am

    Edit/Note from Bob Horn: This answer was accepted because of the EDIT below, specifying a bug in EF. The first part of this answer didn’t work.

    By using object.Equals(object, object) as method of comparing two value types (Nullables are value types too), you are implicitly boxing them. Entity Framework does not support this.

    Try using the == operator instead:

    // Since scan.RawMaterialLabel.ManufacturerId and manufacturerId are both Nullable<T> of the 
    // same type the '==' operator should assert value equality, whether they have a value, or not.
    
    // (int?)1 == (int?)1
    // (int?)null == (int?)null
    // (int?)1 != (int?)null
    
    predicate = predicate.And(scan => scan.RawMaterialLabel.ManufacturerId == manufacturerId);
    

    For value types the == operator asserts value equality, similarly to what the object.Equals() does for reference types.

    EDIT:

    Upon further investigation, there seems to be a bug in older versions of EF (pre EF5).

    Hacky fix with your current version:

    predicate = predicate.And(scan => 
       manufacturerId.HasValue
         ? scan.RawMaterialLabel.ManufacturerId == manufacturerId
         : scan.RawMaterialLabel.ManufacturerId == null);
    

    But if your application allows for it, upgrade to EF5.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for the lengthy code below, my code is able to execute and run
I need to be able to interrogate a function/method and get a list of
not able to get this, can someone help for this LINQ query? select col1,
I am able to get the current date, but I don't know how to
I am able to get the output of a pdf using fpdf, the problem
I'm not able to get this persistence file correct... I do not find any
I'm able to get the following results with this working query: $query = SELECT
I'm trying to run the example from this question about using the BMC remedy
When I'm writing code inside the 'Cocoa Application' I am able to get away
Can you tell me why this isn't working? The code below outputs student1 &

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.