Entity Framework Feature Suggestions

Incorrect handling of null variables in 'where' clause

Problem Description:
The following LINQ-to-Entites query will return the expected results:
var result = from o in table
where o.column == null
select entry;

However, the following query will never return any results:
int? myNullInt = null;
var result = from o in table
where o.column == myNullInt
select entry;

This is because the first query is correctly generating a 'column IS INT' query, while the second one generates a 'column = @p_linq_p; @p_linq_p = NULL'.

This problem affects both LINQ-to-Entities and LINQ-to-SQL. However, while LINQ-to-SQL has the not-so-bad workaround
where o.column.Equals(myNullInt)
The workaround for LINQ-to-Entities is much harder to follow and more painful to write:
where (myNullInt == null ? o.column == null : o.column == myNullInt)

395 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    Mike FlaskoAdminMike Flasko (Lead Program Manager, DataFx) shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →
    completed  ·  Diego VegaAdminDiego Vega (Admin, DataFx) responded  · 

    UPDATE: The SQL CE issue has been fixed in the EF6 provider and we changed the default behavior of EF6 to compensate for three-valued logic.

    UPDATE: changing the status from completed to started. Turns out that SQL Server Compact provider for EF is incompatible with this feature, therefore we are not making this option the default in DbContext API. The option is still available going through the ObjectContext API.

    The option to compensate for three-valued logic used in null equality comparisons performed by SQL databases will be included in the update of the EF core libraries that will ship as part of .NET 4.5. When enabled, this option will cause a LINQ expression such as ‘a == b’ to be translated to an equivalent SQL expression that will evaluate to true when both ‘a’ and ‘b’ are simultaneously null.

    Since the option will cause a change in query behavior that could potentially break existing applications and could also have performance impact in specific scenarios, it will be disabled by default for the ObjectContext API. However, the plan is to enable it by default for the DbContext API in the new version of Entity Framework (i.e. the EntityFramework package in NuGet) that we will release alongside .NET 4.5.

    We encourage you to add or vote for any other suggestions mentioned in the comments in separate items.

    21 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...
      ← Previous 1

      Feedback and Knowledge Base