Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I query for null values in entity framework?

I want to execute a query like this

   var result = from entry in table                      where entry.something == null                      select entry; 

and get an IS NULL generated.

Edited: After the first two answers i feel the need to clarify that I'm using Entity Framework and not Linq to SQL. The object.Equals() method does not seem to work in EF.

Edit no.2: The above query works as intended. It correctly generates IS NULL. My production code however was

value = null; var result = from entry in table                          where entry.something == value                          select entry; 

and the generated SQL was something = @p; @p = NULL. It seems that EF correctly translates the constant expression but if a variable is involved it treats it just like a normal comparison. Makes sense actually. I will close this question.

like image 549
Adrian Zanescu Avatar asked Mar 25 '09 16:03

Adrian Zanescu


People also ask

Is NULL in EF core?

EF will create a NOT NULL column in a database table for a property on which the Required attribute is applied. In the above code, the Required attribute is applied to the StudentName property. So, EF API will create a NOT NULL StudentName column in the Students table, as shown below.

How do you handle NULL values in LINQ query?

An object collection such as an IEnumerable<T> can contain elements whose value is null. If a source collection is null or contains an element whose value is null , and your query doesn't handle null values, a NullReferenceException will be thrown when you execute the query. var query1 = from c in categories where c !=

How do you display NULL?

From the Data menu, choose Report Data Options. The Report Data Options dialog box opens. Under Categories, expand Display, and then select Null Values. Display – Null Values appears on the right side of the editor.


1 Answers

Workaround for Linq-to-SQL:

var result = from entry in table              where entry.something.Equals(value)              select entry; 

Workaround for Linq-to-Entities (ouch!):

var result = from entry in table              where (value == null ? entry.something == null : entry.something == value)              select entry; 

This is a nasty bug which has bitten me several times. If this bug has affected you too, please visit the bug report on UserVoice and let Microsoft know that this bug has affected you as well.


Edit: This bug is being fixed in EF 4.5! Thanks everyone for upvoting this bug!

For backwards compatibility, it will be opt-in - you need manually enable a setting to make entry == value work. No word yet on what this setting is. Stay tuned!


Edit 2: According to this post by the EF team, this issue has been fixed in EF6! Woohoo!

We changed the default behavior of EF6 to compensate for three-valued logic.

This means that existing code that relies on the old behavior (null != null, but only when comparing to a variable) will either need to be changed to not rely on that behavior, or set UseCSharpNullComparisonBehavior to false to use the old broken behavior.

like image 140
BlueRaja - Danny Pflughoeft Avatar answered Oct 02 '22 12:10

BlueRaja - Danny Pflughoeft