Skip to content

CompareTo against a null range in an EF predicate returns no rows; memory returns every row #220

Closed

Description

@CaffeinatedCoder

Problem

CompareTo against a null range in an EF predicate returns no rows, while the same predicate in memory returns every row. The docs say "null sorts before every range" (docs/ranges.md, range order). EF Core's generic CompareTo rewrite turns r.Seats.CompareTo(probe) > 0 into WHERE r."Seats" > NULL, which is never true. >= behaves the same. Found by the pre-tag analysis (#179) on origin/main 3ce68d9. A live probe printed server rows = 0, memory rows = 1.

This breaks claim E45-compareto-runs-as-comparison-operator ("the rows returned match the same predicate … in memory").

Change

One of:

  • rewrite a CompareTo comparison whose operand may be null so it agrees with memory (x > NULL is true for every non-null x in memory, so > 0 and >= 0 become col > x OR x IS NULL, and < 0, <= 0, == 0 with a null operand become false), in the query interceptor where the other range rewrites live; or
  • document that a null operand compares as SQL NULL in a query, next to the ORDER BY NULLS note, and narrow E45 to say so.

The first keeps the "same answer in memory and in SQL" promise. The second is honest but adds a carve-out.

Done when

RangeOrderParityTests.CompareToInAQuery_ReturnsTheRowsMemoryReturns has a null-operand case for every comparison, which fails with the fix reverted (or, for the documentation route, E45 and the doc state the SQL rule and a test holds it).

Activity

  1. added this to the 10.0.0 milestone on Oct 18, 2026
  2. added
    bug
    silent
    area/ranges
    package/efcore
    severity/low
    on Oct 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/rangesbugpackage/efcoreseverity/lowsilent

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions