Skip to content

Fix collection Contains normalization element type and overload matching (#39329) - #39359

Open
SkyDevLab wants to merge 2 commits into
dotnet:mainfrom
SkyDevLab:fix/39329-collection-contains-element-type
Open

SkyDevLab wants to merge 2 commits into
dotnet:mainfrom
SkyDevLab:fix/39329-collection-contains-element-type

Conversation

@SkyDevLab

Copy link
Copy Markdown
Contributor

Fixes #39329

Description

This PR fixes two bugs in QueryableMethodNormalizingExpressionVisitor when normalizing collection.Contains(...) calls:

1. Element type resolution for multi-generic collections

Previously, TryConvertCollectionContainsToQueryableContains extracted 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] picked TKey instead of TItem, causing Expression.Call to throw ArgumentException when converting collection to IEnumerable<sourceType>.

We now pass the element type resolved by TryGetElementType(typeof(ICollection<>)) / TryGetElementType(typeof(IReadOnlyCollection<>)) directly into TryConvertCollectionContainsToQueryableContains(methodCallExpression, elementType).

2. Overload parameter type checking

Previously, any method named Contains on a generic type implementing ICollection<> / IReadOnlyCollection<> was rewritten to Queryable.Contains without 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 in Expression.Call with ArgumentException.

Now, we check:
csharp method.GetParameters() is [{ ParameterType: var parameterType }] && parameterType == elementType
Any other Contains overload whose parameter type does not match the element type is left untouched for downstream translators or provider pipeline processing.

3. Tests

  • Added QueryableMethodNormalizingExpressionVisitorTest in EFCore.Tests testing multi-type argument generic collections and overload matching (100% passing).
  • Added Parameter_collection_KeyedCollectionWithContains_of_ints_Contains_int in PrimitiveCollectionsQueryTestBase.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new shared test lacks overrides in five provider suites, causing their existing override guards to fail when run.

1 open issue
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 Contains methods 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

Comment on lines +342 to +343
[Fact]
public virtual async Task Parameter_collection_KeyedCollectionWithContains_of_ints_Contains_int()
@AndriySvyryd

Copy link
Copy Markdown
Member

Marking the PR as draft until it's updated

@AndriySvyryd
AndriySvyryd marked this pull request as draft November 13, 2026 05:28
@AndriySvyryd

Copy link
Copy Markdown
Member

@SkyDevLab Are you still planning to work on this?

@SkyDevLab

Copy link
Copy Markdown
Contributor Author

Added the missing provider overrides for Parameter_collection_KeyedCollectionWithContains_of_ints_Contains_int across all 5 provider test classes:

  • PrimitiveCollectionsQuerySqliteTest
  • PrimitiveCollectionsQueryCosmosTest
  • PrimitiveCollectionsQuerySqlServerTest
  • PrimitiveCollectionsQueryOldSqlServerTest
  • PrimitiveCollectionsQuerySqlServerJsonTypeTest

Verified Check_all_tests_overridden and Parameter_collection_KeyedCollectionWithContains_of_ints_Contains_int pass on SQLite and SQL Server (and Cosmos compiles cleanly). PR is ready for review!

@SkyDevLab
SkyDevLab marked this pull request as ready for review November 26, 2026 19:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Collection Contains normalization rewrites any Contains overload and guesses the element type from the first type argument

3 participants