In Entity Framework, check for null values in Linq queries.

asp.net entity-framework linq null

Question

How can I compare integer type null values Where portion of Linq query in .Net Entity framework 4.1?

1
2
1/18/2012 3:41:20 PM

Accepted Answer

You can only compare an int to NULL if the int is nullable. If not, the default value for int will be 0 and never null.

You define a nullable int property like this:

int? value { get; set; }

And check it like this:

if ( value != null )
{
   int x = value.Value;
}

In the where clause of a Linq query it would be

var result = from d in data
             where d.Value != null
             select d
11
1/18/2012 3:43:37 PM

Popular Answer

If you are comparing to a null value, you must first compare your value to null due to a bug.

var field = from field in table
            where (value == null ? field.property == null : field.property == value)
            select field;


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow