Charles
Charles

Reputation: 716

Where using "IS NULL"

Im trying to retrieve a set of records using EF 4.3.1. I need to get all those records that have a NULL value in certain column.

I checked the database and many rows are null in that collumn, also the column is nullable. EF is doing something strange. Looking the profiler, it is generating the following SQL:

SELECT 
CAST(NULL AS int) AS [C1]
FROM  ( SELECT 1 AS X ) AS [SingleRowTable1]
WHERE 1 = 0

My entity columns is declared as "int?"

I tried using the following conbinations but i get the same result:

dbset.Where(e=>e.MyNullableInt == null).ToList();
dbset.Where(e=>e.MyNullableInt.Equals(null)).ToList();

The only way that i can pass this strange behavior is load the full set, populate a variable and do the where after this step, which is very inneficient since it loads all the data.

Any ideas how to achieve this?.

Upvotes: 0

Views: 199

Answers (2)

Asheesh Kumar
Asheesh Kumar

Reputation: 161

Check your mapping as i have seen this happening when in mapping you are somehow making that field Required.

or in your database that field is not nullable.

Upvotes: 1

Nowshath
Nowshath

Reputation: 842

Try This may this can Help U...

dbset.Where(e=> (e.MyNullableInt ?? -1) == -1).ToList();

Upvotes: 1

Related Questions