ISNULL equivalent in Linq

I have a table with a column. IsActiveNow I want to get a list of records with a given status, but I want to deal IsActivewith null values ​​as false. In SQL, we used:

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

How to do this with LINQ?

PS: I use LINQ for Entities, so I cannot use a function to create a value, for example:

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

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

How could I think:

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

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

+4
source share
1 answer

Instead, ISNULLyou can use the operator ??:

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

Source: https://habr.com/ru/post/1629776/


All Articles