Skip to content

Allow expression row value comparisons via extension methods #2349

Description

@roji

See dotnet/efcore#26822 - this implements that but for PG only.

Most databases allow comparing row value expressions:

WHERE (a, b) > (3, 4)
-- Equivalent to:
WHERE a > 3 OR (a = 3 AND b > 4)

This is is notably useful for keyset pagination with multiple keys (see this detailed presentation).

I prevently implemented #2111 and reverted a bad approach, which pattern-matched the expanded form above in the tree, and replaced it with the row value expression. This was both brittle (some tree shapes failed to be matched), and also results in complicated LINQ queries.

Ideally, users would be able to express row value comparisons in LINQ queries by using value tuples (see dotnet/efcore#26822):

ctx.Blogs.Where(b => EF.Functions.GreaterThan((b.X, b.Y), (3, 4)))

However, since value tuple syntax isn't yet supported in C# expression trees, we can allow object arrays instead:

ctx.Blogs.Where(b => EF.Functions.GreaterThan(new object[] { b.X, b.Y }, new object[] { 3, 4 }))

/cc @mrahhal

Activity

  1. added this to the 7.0.0 milestone on May 4, 2022
  2. self-assigned this
    on May 4, 2022
  3. added 5 commits that reference this issue on May 4, 2022
    7a95327
    e27ad0b
    024ff54
    f869f88
    db16332
  4. roji commented on May 6, 2022

    @roji
    MemberAuthor

    OK, I ended up going in a slightly different direction, and using ValueType<T1, T2...>(x, y...) instead of arrays to express row values in LINQ:

    _ = await ctx.Customers
        .Where(c => EF.Functions.GreaterThan(
            new ValueTuple<string, string>(c.City, c.CustomerID),
            new ValueTuple<string, string>("Buenos Aires", "OCEAN")))
        .CountAsync();

    This is a bit uglier/more verbose, but:

    • Tuples really are the appropriate .NET construct for mapping a row value.
    • When some day ValueTuple syntax becomes supported in LINQ expression trees ((x, y)), this should transition seamlessly.
    • Doesn't rely on provider array support which is PG-specific, so should be easily portable to the general relational layer if we want.
    • Avoids any confusion with arrays, which are also supported in PG. For example, arrays can also be compared in PG (very different thing from row value comparisons).
    • We can selectively make NewExpression for ValueTuple non-evaluatable, rather than NewArrayExpression, which is used in other contexts where it can be evaluatable (see Support for row value comparisons #2350 (comment)).

    /cc @smitpatel

  5. changed the title [-]Allow expression row value comparisons via extension method with object array parameters[/-] [+]Allow expression row value comparisons via extension methods[/+] on May 6, 2022
  6. added 2 commits that reference this issue on May 6, 2022
    47656a0
    253c168
  7. added 2 commits that reference this issue on May 7, 2022
    52f68c3
    511a6af
  8. roji commented on May 8, 2022

    @roji
    MemberAuthor

    Docs: npgsql/doc@511a6af in the 7.0 branch.

  9. added a commit that references this issue on May 17, 2022
  10. added a commit that references this issue on Jun 5, 2022
  11. added a commit that references this issue on Jul 10, 2022
  12. added theissue type on Jun 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions