Here’s a simple LINQ query:
var objs = db.Objects.Where(o => o.Field1 == val);
This translates to SQL query:
select * from [Object] where Field1 = @p1
Trouble is, the value of val can also legitimately be null. And SQL doesn’t like comparing nulls; it insists on the syntax ... where Field1 is null.
Is there any way of doing this neatly, short of using a ?? / isnull operation?
This, again, is an EF weakness in LINQ support. Good old LINQ to SQL translated this properly depending on the runtime value of val.
I suggest you go with this:
If EF translates this litterally, the SQL Server query optimizer will actually pick up this pattern and optimize it to an “equals-with-nulls” check. You can even seek indexes using this code pattern, it just works. In the query plans this shows up as
ISin contrast toEQ.