Aug 18, 2009

Checking NULL in LINQ and SQL

I stepped on a bit field in my table which has NULL as default value.

Checking in my LINQ code for not true condition as below just failed:
p.IsValidLicense != true
The row in the table had the value NULL and therefore i expected my query to return true, but in vain.

Then i executed it as SQL query in SSMS as below. Again unexpected result.
Where IsValidLicense <> 1
Time for google. Seems, the NULL issue has caused lot of miseries in the lives of a lot of people. Everyone just jumped in to answer my question.

The correct way to construct the query..

In SQL:
(IsValidLicense IS NULL OR IsValidLicense = 0)
In LINQ:
(p.IsValidLicense == null || p.IsValidLicense == false)
Hope this helps!
Reblog this post [with Zemanta]

No comments:

Post a Comment