Ashkan Mobayen Khiabani Ashkan Mobayen Khiabani - 1 month ago 10x
C# Question

ISNULL equivalent in Linq

I have a table with a column

now I want to get a list of records with given status but I want to deal with
with null values as false.
In SQL we used:

SELECT * FROM dbo.Table c WHERE ISNULL(IsActive, 0) = @act

How can in do this with LINQ?

P.S.: I'm using LINQ to Entities so I can't use a function to cast the value like:

Table.Where(t=> Parse(t.IsActive) == act)

bool Parse(bool? val){
return val == true? true : false;

The way I could think of is:

Table.Where(t=> (t.IsActive==null || t.IsActive == false) && act == false)
|| (t.IsActive==true && act == true));

But this solution seems to be very poor and I think there should be a better way.


You can use ?? operator instead of ISNULL:

Table.Where(t=> (t.IsActive ?? false) == act);