LINQ to SQL Null check in Where Clause

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! :)

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

 

Visual Studio 2008 - Clear Recent Projects List

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.

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

 

Flex Scrollbar - Remove up - down arrows

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 );
}

 

Currently rated 3.4 by 5 people

  • Currently 3.4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

 

Enum as the data source for drop-down list - ASP.NET

I've been using enum's a lot lately. Here is another enum snippet for my snippet collection showing how to use an enum as the source for the items in a drop down.

enum myEnum {Jan, Feb, Mar};

DropDownList myDDL;

myDDL.DataSource = Enum.GetNames( typeof ( myEnum ) );

myDDL.DataBind();

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

 

Search


podcast feed

free flex trial

categories

Language Translation

Advertisement

blog roll

Subscribe

Recent comments

Tag Cloud

calendar

<<  July 2008  >>
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910