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

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

28. November 2007

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

snippets

 Convert a String to Enumeration in C#

21. November 2007
String myString = "one";

enum myEnum {one, two, three};

myEnum enumeration = (myEnum)Enum.Parse(typeof(myEnum),myString,true);

snippets

 Cool Little Script

20. November 2007

I just stumbled upon this little javascript that spins the image on a page in spiral. It looks pretty cool on my blog with all the rss icons and feed chicklets spinning.

Click here to see it in action.

Here is the script you can paste in your browser address bar with any web page loaded:

javascript:R=0; x1=.1; y1=.05; x2=.25; y2=.24; x3=1.6; y3=.24; x4=300; y4=200; x5=300; y5=200; DI= document.images; DIL=DI.length; function A(){for(i=0; i<DIL; i++){DIS=DI[ i ].style; DIS.position='absolute'; DIS.left=Math.sin(R*x1+i*x2+x3)*x4+x5; DIS.top=Math.cos(R*y1+i*y2+y3)*y4+y5}R++}setInterval('A()',5 ); void(0)

snippets

 SQL Server 2005 : List columns alphabetically

11. October 2007

I'm gonna start a new category today called 'snippets'. I'm going to use this category to post helpful little snippets of code, markup, SQL or whatever that I use often while developing new software. This will be mostly for my own purposes, but it will be up here on my blog for all to see. In time it'll be a nice little reference.

In SQL Server 2005, the object explorer lists a tables columns in the same order they are entered in the tables schema. For large tables, this can make it difficult to quickly find exaclty what a column is named, and to get its type, etc. To get a alphabetical list, you can query against the information_schema.columns system view:

SELECT column_name, data_type, character_maximum_length

FROM information_schema.columns

WHERE table_name = 'myTable' ORDER BY column_name

snippets