Repository navigation
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
The new shared test lacks overrides in five provider suites, causing their existing override guards to fail when run.
What changed in this PR
Fixes #39329 in EF Core’s query normalization by resolving collection element types correctly and leaving unrelated Contains overloads available for downstream translation.
Changes:
- Uses the collection interface’s element type instead of the declaring type’s first generic argument.
- Normalizes only single-parameter
Containsmethods whose parameter matches the element type. - Adds focused normalization tests and a shared query regression test for multi-generic collections.
| File | Description |
|---|---|
test/EFCore.Tests/Query/Internal/QueryableMethodNormalizingExpressionVisitorTest.cs |
Tests element-type resolution and overload matching for mutable and read-only collections. |
test/EFCore.Specification.Tests/Query/PrimitiveCollectionsQueryTestBase.cs |
Adds a multi-generic collection regression test covering positive and negated membership. |
src/EFCore/Query/Internal/QueryableMethodNormalizingExpressionVisitor.cs |
Corrects element-type selection and checks overload parameters before normalization. |
Review effort: Balanced
| [Fact] | ||
| public virtual async Task Parameter_collection_KeyedCollectionWithContains_of_ints_Contains_int() |
|
Marking the PR as draft until it's updated |
|
@SkyDevLab Are you still planning to work on this? |
|
Added the missing provider overrides for
Verified |

Fixes #39329
Description
This PR fixes two bugs in
QueryableMethodNormalizingExpressionVisitorwhen normalizingcollection.Contains(...)calls:1. Element type resolution for multi-generic collections
Previously,
TryConvertCollectionContainsToQueryableContainsextracted the element type via:csharp var sourceType = methodCallExpression.Method.DeclaringType!.GetGenericArguments()[0];For collections with multiple type parameters (such as
Keyed<TKey, TItem> : ICollection<TItem>),GetGenericArguments()[0]pickedTKeyinstead ofTItem, causingExpression.Callto throwArgumentExceptionwhen convertingcollectiontoIEnumerable<sourceType>.We now pass the element type resolved by
TryGetElementType(typeof(ICollection<>))/TryGetElementType(typeof(IReadOnlyCollection<>))directly intoTryConvertCollectionContainsToQueryableContains(methodCallExpression, elementType).2. Overload parameter type checking
Previously, any method named
Containson a generic type implementingICollection<>/IReadOnlyCollection<>was rewritten toQueryable.Containswithout checking whether its parameter matches the collection's element type. If a collection defines an additional overload (e.g.,SpanSet<TSpan, T>.Contains(T value)), query preprocessing crashed inExpression.CallwithArgumentException.Now, we check:
csharp method.GetParameters() is [{ ParameterType: var parameterType }] && parameterType == elementTypeAny other
Containsoverload whose parameter type does not match the element type is left untouched for downstream translators or provider pipeline processing.3. Tests
QueryableMethodNormalizingExpressionVisitorTestinEFCore.Teststesting multi-type argument generic collections and overload matching (100% passing).Parameter_collection_KeyedCollectionWithContains_of_ints_Contains_intinPrimitiveCollectionsQueryTestBase.