03/04/2009
LINQ Queries Containing Null Values for Nullable in a Where Clause
Posted by
Osvel Legon
How does LINQ to SQL handle the nullable type in the where clause?
The current .Where expression does not support the use of == between Nullable types when there is a null value, so here it's a trick to solve that:
Use object.Equals to get 2-value equals semantics (nulls equal nulls).
from c in db.ProductCategories
where object.Equals(c.ParentProductCategoryID, myID)
select c;
If the compared value is null LINQ will generate c.ParentProductCategoryID IS NULL otherwise it will generate c.ParentProductCategoryID = myID.
« Back to Blog Main Page
|