Linq to Entities Querying for Nullable Types

I have a script in which we do a soft delete in rows in a database. I want to include rows that are not deleted. How can I achieve this with LINQ.

Let's say

from c in context.ASDSet
where (c => c.DeletedFlag.HasValue && !c.DeletedFlag.Value)

But I could not achieve the result.

I want the resulting SQL to look like:

select *  from table where IsNull(column, 0) = 0
+3
source share
1 answer

It sounds like you really want to:

var query = Context.ASDSet.Where(c => c.DeletedFlag == null || 
                                      c.DeletedFlag.Value == false);

In other words, this includes lines where the flag is null, while your current query excludes lines where the flag is null.

+7
source

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


All Articles