We have an EDMX-based EF6 application, where we'd like to run a LINQ query like this.
string category = ... // comes from somewhere and equals to null
context.Product.Where(e => e.Category == category).ToArray();
The problem I see is that the generated SQL contains a [table].[Category] = @p... in the where clause, regardless of the variable category being null or not. So at the end of the day the query returns no results if the variable is null, instead of generating a proper is null criteria and return the proper rows.
I've tested with the explicit e => e.Category == null expression and that does generate the is null as expected.
I've also checked the EDMX and the SQL, and the corresponding Category column is nullable indeed in both places, so that might not be the issue.
Any help would be appreciated.
Category = @p1is very different fromCategory = @p1 or Category IS NULLtoo, resulting in very different execution plans and performance. EF (or any ORM) can't arbitrarily choose the wider query over the narrower one, as it would return unexpected results and result in far worse performance. You could argue there should be a switch for people that expect an ORM to not use SQL semantics, but that would result in a maintenance nightmare when other developers try to understand why a query doesn't behave as expectedcategoryis basicallynullso it should generate a[Category] is nullwhere clause this case. It seems like EF does not evaluate such expressions locally but just generate a parameterized query.