Search code examples
c#sql-serverlinqlinq-to-entitiesnullable

Nullable Bit field true or false detection


If I have a nullable bit field (named 'Disabled') and the data is as follows:

ID | Name | Disabled
--------------------
1  | Mine | null
2  | Yours| 1

If I then execute the following Linq To Entities statement no values are returned:

from r in Rates
where r.Disabled != true
select r

But If I execute this Linq To Entities statement:

from r in Rates
where r.Disabled == true
select r

It returns the expected one row with an ID of 2.

I want the first statement to return row 1 where the value of the 'Disabled' field is null.

Am I missing a trick here? Why does the first statement not return the null valued row?

Edit let me rephrase the question... Why isn't it returning the null rows? I know I can put in a simple null check.


Solution

  • from r in Rates
    where r.Disabled != true
       || r.Disabled IS NULL
    select r