Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to #35024 - Query could not be translated when using a static ICollection/IList field #35029

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/EFCore/Query/QueryRootProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected override Expression VisitMethodCall(MethodCallExpression methodCallExp

private Expression VisitQueryRootCandidate(Expression expression, Type elementClrType)
{
switch (expression)
switch (RemoveConvert(expression))
{
// An array containing only constants is represented as a ConstantExpression with the array as the value.
// Convert that into a NewArrayExpression for use with InlineQueryRootExpression
Expand Down Expand Up @@ -122,6 +122,11 @@ when listInitExpression.Type.TryGetElementType(typeof(IList<>)) is not null
default:
return Visit(expression);
}

static Expression RemoveConvert(Expression e)
=> e is UnaryExpression { NodeType: ExpressionType.Convert or ExpressionType.ConvertChecked } unary
? RemoveConvert(unary.Operand)
: e;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2068,6 +2068,20 @@ public override async Task Contains_with_local_anonymous_type_array_closure(bool
AssertSql();
}

public override Task Contains_with_static_IList(bool async)
=> Fixture.NoSyncTest(
async, async a =>
{
await base.Contains_with_static_IList(a);

AssertSql(
"""
SELECT VALUE c
FROM root c
WHERE c["id"] IN ("ALFKI", "ANATR")
""");
});

public override async Task OfType_Select(bool async)
{
// Contains over subquery. Issue #17246.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1361,6 +1361,15 @@ public virtual Task Contains_with_local_anonymous_type_array_closure(bool async)
ss => ss.Set<OrderDetail>().Where(o => ids.Contains(new { Id1 = o.OrderID, Id2 = o.ProductID })));
}

private static readonly IList<string> StaticIds = new List<string> { "ALFKI", "ANATR" };

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Contains_with_static_IList(bool async)
Copy link
Member

Choose a reason for hiding this comment

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

Is there a specific reason this is in this test class (there doesn't seem to be anything aggregate about the query).

Maybe PrimitiveCollectionsQueryTestBase is a better place...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

there is a lot of Contains tests in that class, so just keeping them all together. I don't mind moving them all to PCQTB as I do agree it's better place for them.

Copy link
Member

Choose a reason for hiding this comment

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

Not very important obviously... Whatever you prefer

=> AssertQuery(
async,
ss => ss.Set<Customer>().Where(c => StaticIds.Contains(c.CustomerID)));

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task OfType_Select(bool async)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2303,6 +2303,18 @@ public override async Task Contains_with_local_anonymous_type_array_closure(bool
AssertSql();
}

public override async Task Contains_with_static_IList(bool async)
{
await base.Contains_with_static_IList(async);

AssertSql(
"""
SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region]
FROM [Customers] AS [c]
WHERE [c].[CustomerID] IN (N'ALFKI', N'ANATR')
""");
}

public override async Task OfType_Select(bool async)
{
await base.OfType_Select(async);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,18 @@ public override async Task Contains_with_local_anonymous_type_array_closure(bool
public override async Task Contains_with_local_tuple_array_closure(bool async)
=> await AssertTranslationFailed(() => base.Contains_with_local_tuple_array_closure(async));

public override async Task Contains_with_static_IList(bool async)
{
await base.Contains_with_static_IList(async);

AssertSql(
"""
SELECT "c"."CustomerID", "c"."Address", "c"."City", "c"."CompanyName", "c"."ContactName", "c"."ContactTitle", "c"."Country", "c"."Fax", "c"."Phone", "c"."PostalCode", "c"."Region"
FROM "Customers" AS "c"
WHERE "c"."CustomerID" IN ('ALFKI', 'ANATR')
""");
}

public override async Task Contains_inside_aggregate_function_with_GroupBy(bool async)
{
await base.Contains_inside_aggregate_function_with_GroupBy(async);
Expand Down