Rob L
Rob L

Reputation: 2372

LINQ Selecting rows where a column value IS NULL

var myRows = myContext.MyTable
    .Where(v => v.MyColumn == null);

This does not select any rows from the table, even though there are many rows where "MyColumn is null"?

I need to select the rows where MyColumn contains null.

I realise that in T-SQL I'd have to use the "IS NULL" operator, but how can I select the rows I want in Linq?

I am using Entity Framework 5.0.

If this should be working, let me know, as my bug must be elsewhere.

Thanks

Upvotes: 2

Views: 5761

Answers (2)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726987

One common reason why this may not be working is if you map a nullable DB column (say, MyColumn INTEGER NULL) to a non-nullable field of a C# class (say, int MyColumn instead of int? MyColumn).

Make sure that MyColumn is of nullable type to fix this problem: other than that, your expression is correct, EF will correctly translate it to IS NULL.

Upvotes: 3

Ronan Thibaudau
Ronan Thibaudau

Reputation: 3603

Yes it should be working as is Edit : Potential thing to check, is the column correctly noted as nullable in your edmx? I'm guessing if not it could be an issue

Upvotes: 1

Related Questions