Skip to content

Translate element-wise predicates over a value set column: Any, All, SelectMany (unnest) #95

Open

Description

@CaffeinatedCoder

unnest(anyarray) → SETOF anyelement is the array function that turns an element-wise question into SQL. A value set deliberately does not implement IEnumerable<T> (so EF never treats it as a primitive collection), which also means EF has nothing to translate for:

bookings.Where(b => b.Tags.Any(t => t.StartsWith("urgent-")))   // a pattern over elements
bookings.Where(b => b.Codes.All(c => c < 100))
bookings.SelectMany(b => b.Tags)                                 // one row per element

Contains(value) covers the equality case (@> ARRAY[v]), and the @>/<@/&& family covers set-to-set questions, but any predicate over the element that is not equality has no server-side form today. In a Where it throws (correct, fails loudly).

Proposed API: nothing new on the set; the duck-typed GetEnumerator already allows foreach. For LINQ, either a Values projection (b.Tags.Values.Any(…), ImmutableArray<T>) which EF can see, or Any/All/Count(predicate)/SelectMany recognised directly on the set type by ValueSetsMethodCallTranslator.

Translation: EXISTS (SELECT 1 FROM unnest(b."Tags") AS t(value) WHERE t.value LIKE 'urgent-%'); SelectMany → CROSS JOIN LATERAL unnest(…). The element gets the family's element mapping (BridgedElementTypeMapping for wrapper arities), so a wrapper set's predicate compares the primitive.

Guard note: unnest of a value derived from array_cat is fine (no order or multiplicity dependence for Any/All), but Count(predicate) over one is not; ValueSetSqlGuard needs the new shape in its allow/refuse lists.

Done when: the three shapes translate, the SQL is asserted in SetQueryTranslationTests, a live test agrees with memory, and docs/efcore.md#what-runs-where lists them.

Found by the PostgreSQL catalogue sweep (pg_proc over anyarray), 2026-10-17.

Activity

  1. added
    enhancement
    area/value-sets
    package/efcore
    on Oct 17, 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/value-setsenhancementpackage/efcore

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions