Repository navigation
Description
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
Fromcall over a span conversion of an array into theIEnumerable<T>overload before EF Core evaluates parameters. It already rewrites calls before translation (RangeSetContainsRewriter). docs/efcore.mdsays 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
fix: build a set from an inline array in a query (#583)
Merge pull request #591 from CaffeinatedCoder/fix/583-inline-set-from…
Merge pull request #609 from CaffeinatedCoder/fix/607-inline-set-from…