The particular situation I am currently trying to get around involves a bool? referencing a nullable bit field in the database. But the logic should apply to many different cases.
I am using Entity Framework and have an object with a bool? property. I am starting to write a lot of Linq around this particular property, but because Sql doesnt retrieve null values when bool? != true is written, I am finding I am writing queries that look like:
var Result = from f in FooList
where f.NullableBool == false || f.NullableBool == null
where f.Bar.Contains(SearchTerm)
select f;
This isn’t a massive problem, but I would really like to be able to remove the “x.NullableBool == false || x.NullableBool == false” to a different place and instead call something like:
var Result = from f in FooList
where f.IsNot && f.Bar.Contains(SearchTerm)
select f;
I have tried adding properties and methods of types like ‘Expression < Func < Foo, bool > >’, ‘Func < Foo, bool >’ and ‘bool’ to the Foo class but Linq doesn’t seem to like them.
Any pointers greatly appreciated.
Thanks
An extension method of
IQueryableshould work:Then you can use it this way: