LINQ to SQL Null check in Where Clause

25. June 2008

After testing several LINQ queries for ways to do a null comparison in the where clause, I found only one way to do it that returns the result set as though I had used "IS NULL" in a standard SQL query. The key is to use the object.Equals() method:

   1:private ProductDataContext db = new  ProductDataContext();
   2:   
   3:/// <summary>
   4:/// Return all products for a given manufacturer
   5:/// </summary>
   6:/// <param name="manufacturerName"></param>
   7:/// <returns></returns>
   8:public IQueryable<Product> GetProdByManu(String manufacturerName, int? catId)
   9:{
  10:   
  11:    var products = from o in db.Products
  12:                   where o.manufacturerName == manufacturerName
  13:                   && object.Equals(o.categoryID, catId)
  14:                   select o;
  15:             
  16:   
  17:    if(products.Count() > 0)
  18:    {
  19:        return products;
  20:    }
  21:    else
  22:    {
  23:        return null;
  24:    }
  25:}

 

The above method GetProductsByManu accepts a parameter catID , a nullable integer. When calling the method if I pass null as the value for catID , LINQ will correctly return products where categoryID is null in the database.  I find that if I use == null as show below, the results are inaccurate:

   1:    var products = from o in db.Products
   2:                             where o.manufacturerName == manufacturerName
   3:                             && o.categoryID == null
   4:                             select o;
 

 

In the above case, even though my tables contain products with a null categoryID, those products are not returned by LINQ. I'm not sure exactly why this is, and maybe someone with more LINQ experience than I can shed some light in the comments. I just know from now on, when doing a check for null in a LINQ where clause, object.Equals() is what I'll be using.

Hope this helps someone else with the same issues I had! :)

snippets

 Visual Studio 2008 - Clear Recent Projects List

27. May 2008

Have you ever wanted to clear your "Recent Projects" list on your Visual Studio 2008 start page? If so here's a .reg file that will do it for you:


 Windows Registry Editor Version 5.00

[-HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\ProjectMRUList]
[HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\ProjectMRUList]


Just copy the above contents into a new file named anything you like, with a .reg extension. When your Recent Projects list gets cluttered and you want to clear it, just double-click the .reg file you just created and it will clear your list.

snippets

 Flex Scrollbar - Remove up - down arrows

20. May 2008
Add the following CSS to your Flex application to remove up/down arrows from all scrollbars:

 HScrollBar{
up-arrow-skin: ClassReference( null );
down-arrow-skin: ClassReference( null );
}
VScrollBar{
up-arrow-skin: ClassReference( null );
down-arrow-skin: ClassReference( null );
}

 

snippets, Adobe Flex