Skip to content

A set built inline in a query with From(new[] { … }) throws while EF Core evaluates the parameter #583

Done

Description

@CaffeinatedCoder

Found by a pre-release sweep of main @ 577b85e (lens: differential); reproduced in a scratch test. Not new in 11.0.0-preview.2: the two From overloads and C# 14 are on release/10.x too (not run there).

Problem

A set built inline in a query from an array throws when EF Core evaluates the query's parameters:

db.Bookings.Where(b => b.Codes.IsSubsetOf(Int32Set.From(new[] { 1, 2 })))
InvalidOperationException: An exception was thrown while attempting to evaluate a LINQ query parameter expression.
 ---> ArgumentException: GenericArguments[1], 'System.ReadOnlySpan`1[System.Int32]', on
      'System.Linq.Expressions.Interpreter.FuncCallInstruction`2[T0,TRet]' violates the constraint of type 'TRet'.

Every set type has From(params ReadOnlySpan<T>) beside From(IEnumerable<T>) (src/CodoMetis.ValueRanges/Sets/StringSet.cs:47, and the same pair on the range set types). Since C# 14 an array converts to a span implicitly, so From(new[] { … }) binds the span overload, and the expression tree holds a conversion to ReadOnlySpan<T>, which the LINQ interpreter EF Core evaluates parameters with cannot run.

From("a", "b") inline does not compile in an expression tree (CS8640, CS9226), so the array form is the only inline spelling that compiles, and it fails at run time. The message names neither the set nor a way out.

Scenario

Measured on 577b85e in the translation tests (ToQueryString):

Inline operand Result
StringSet.From(new[] { "a", "b" }) throws
Int32Set.From(new[] { 1, 2 }) throws
DateRangeSet.From(new[] { range }) throws
Int32Set.From(new List<int> { 1, 2 }) translates
Int32Set.From((IEnumerable<int>)new[] { 1, 2 }) translates
Int32Set.Parse("{1,2}", null) translates
a captured var set = Int32Set.From(1, 2) translates

Change

Either of:

  • The query interceptor rewrites a From call over a span conversion of an array into the IEnumerable<T> overload before EF Core evaluates parameters. It already rewrites calls before translation (RangeSetContainsRewriter).
  • docs/efcore.md says to capture the set, or to build it from a list, and why.

The first removes the trap. Check what EF Core itself does for a span-taking method in a query on 10 and 11 before choosing, since it rewrites MemoryExtensions.Contains for the same C# 14 change.

Test

A translation test that hands every discovered set type's From(new[] { … }) to a query as an inline operand and expects the SQL of the captured form. It fails without the change.

Activity

  1. added
    bug
    area/range-set
    area/value-sets
    package/efcore
    severity/low
    on Nov 29, 2026
  2. added this to the 11.0.0 milestone on Nov 30, 2026
  3. added 2 commits that reference this issue on Nov 30, 2026
  4. added a commit that references this issue on Nov 30, 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/range-setarea/value-setsbugpackage/efcoreseverity/low

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions