Skip to content

Commit e294d78

Browse files
committed
Fix implementation.
1 parent 77cfbb8 commit e294d78

6 files changed

+20
-10
lines changed

src/EFCore/Query/Internal/QueryableMethodNormalizingExpressionVisitor.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -492,12 +492,16 @@ private Expression TryConvertCollectionContainsToQueryableContains(MethodCallExp
492492

493493
var sourceType = methodCallExpression.Method.DeclaringType!.GetGenericArguments()[0];
494494

495+
var objectExpression = methodCallExpression.Object!.Type.IsValueType
496+
? Expression.Convert(methodCallExpression.Object!, typeof(IEnumerable<>).MakeGenericType(sourceType))
497+
: methodCallExpression.Object!;
498+
495499
return VisitMethodCall(
496500
Expression.Call(
497501
QueryableMethods.Contains.MakeGenericMethod(sourceType),
498502
Expression.Call(
499503
QueryableMethods.AsQueryable.MakeGenericMethod(sourceType),
500-
methodCallExpression.Object!),
504+
objectExpression),
501505
methodCallExpression.Arguments[0]));
502506
}
503507

src/EFCore/Query/QueryRootProcessor.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,13 @@ protected override Expression VisitMethodCall(MethodCallExpression methodCallExp
8585

8686
private Expression VisitQueryRootCandidate(Expression expression, Type elementClrType)
8787
{
88-
switch (expression)
88+
var candidateExpression = expression;
89+
if (expression is UnaryExpression { NodeType: ExpressionType.Convert } convertExpression
90+
&& convertExpression.Type.GetGenericTypeDefinition() == typeof(IEnumerable<>))
91+
{
92+
candidateExpression = convertExpression.Operand;
93+
}
94+
switch (candidateExpression)
8995
{
9096
// An array containing only constants is represented as a ConstantExpression with the array as the value.
9197
// Convert that into a NewArrayExpression for use with InlineQueryRootExpression

test/EFCore.SqlServer.FunctionalTests/Query/PrimitiveCollectionsQueryOldSqlServerTest.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -504,13 +504,13 @@ public override async Task Parameter_collection_ImmutableArray_of_ints_Contains_
504504

505505
AssertSql(
506506
"""
507-
SELECT [p].[Id], [p].[Bool], [p].[Bools], [p].[DateTime], [p].[DateTimes], [p].[Enum], [p].[Enums], [p].[Int], [p].[Ints], [p].[NullableInt], [p].[NullableInts], [p].[NullableString], [p].[NullableStrings], [p].[String], [p].[Strings]
507+
SELECT [p].[Id], [p].[Bool], [p].[Bools], [p].[DateTime], [p].[DateTimes], [p].[Enum], [p].[Enums], [p].[Int], [p].[Ints], [p].[NullableInt], [p].[NullableInts], [p].[NullableString], [p].[NullableStrings], [p].[NullableWrappedId], [p].[NullableWrappedIdWithNullableComparer], [p].[String], [p].[Strings], [p].[WrappedId]
508508
FROM [PrimitiveCollectionsEntity] AS [p]
509509
WHERE [p].[Int] IN (10, 999)
510510
""",
511511
//
512512
"""
513-
SELECT [p].[Id], [p].[Bool], [p].[Bools], [p].[DateTime], [p].[DateTimes], [p].[Enum], [p].[Enums], [p].[Int], [p].[Ints], [p].[NullableInt], [p].[NullableInts], [p].[NullableString], [p].[NullableStrings], [p].[String], [p].[Strings]
513+
SELECT [p].[Id], [p].[Bool], [p].[Bools], [p].[DateTime], [p].[DateTimes], [p].[Enum], [p].[Enums], [p].[Int], [p].[Ints], [p].[NullableInt], [p].[NullableInts], [p].[NullableString], [p].[NullableStrings], [p].[NullableWrappedId], [p].[NullableWrappedIdWithNullableComparer], [p].[String], [p].[Strings], [p].[WrappedId]
514514
FROM [PrimitiveCollectionsEntity] AS [p]
515515
WHERE [p].[Int] NOT IN (10, 999)
516516
""");

test/EFCore.SqlServer.FunctionalTests/Query/PrimitiveCollectionsQuerySqlServer160Test.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ public override async Task Parameter_collection_ImmutableArray_of_ints_Contains_
533533
"""
534534
@ints='[10,999]' (Nullable = false) (Size = 4000)
535535
536-
SELECT [p].[Id], [p].[Bool], [p].[Bools], [p].[DateTime], [p].[DateTimes], [p].[Enum], [p].[Enums], [p].[Int], [p].[Ints], [p].[NullableInt], [p].[NullableInts], [p].[NullableString], [p].[NullableStrings], [p].[String], [p].[Strings]
536+
SELECT [p].[Id], [p].[Bool], [p].[Bools], [p].[DateTime], [p].[DateTimes], [p].[Enum], [p].[Enums], [p].[Int], [p].[Ints], [p].[NullableInt], [p].[NullableInts], [p].[NullableString], [p].[NullableStrings], [p].[NullableWrappedId], [p].[NullableWrappedIdWithNullableComparer], [p].[String], [p].[Strings], [p].[WrappedId]
537537
FROM [PrimitiveCollectionsEntity] AS [p]
538538
WHERE [p].[Int] IN (
539539
SELECT [i].[value]
@@ -544,7 +544,7 @@ FROM OPENJSON(@ints) WITH ([value] int '$') AS [i]
544544
"""
545545
@ints='[10,999]' (Nullable = false) (Size = 4000)
546546
547-
SELECT [p].[Id], [p].[Bool], [p].[Bools], [p].[DateTime], [p].[DateTimes], [p].[Enum], [p].[Enums], [p].[Int], [p].[Ints], [p].[NullableInt], [p].[NullableInts], [p].[NullableString], [p].[NullableStrings], [p].[String], [p].[Strings]
547+
SELECT [p].[Id], [p].[Bool], [p].[Bools], [p].[DateTime], [p].[DateTimes], [p].[Enum], [p].[Enums], [p].[Int], [p].[Ints], [p].[NullableInt], [p].[NullableInts], [p].[NullableString], [p].[NullableStrings], [p].[NullableWrappedId], [p].[NullableWrappedIdWithNullableComparer], [p].[String], [p].[Strings], [p].[WrappedId]
548548
FROM [PrimitiveCollectionsEntity] AS [p]
549549
WHERE [p].[Int] NOT IN (
550550
SELECT [i].[value]

test/EFCore.SqlServer.FunctionalTests/Query/PrimitiveCollectionsQuerySqlServerTest.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ public override async Task Parameter_collection_ImmutableArray_of_ints_Contains_
556556
"""
557557
@ints='[10,999]' (Nullable = false) (Size = 4000)
558558
559-
SELECT [p].[Id], [p].[Bool], [p].[Bools], [p].[DateTime], [p].[DateTimes], [p].[Enum], [p].[Enums], [p].[Int], [p].[Ints], [p].[NullableInt], [p].[NullableInts], [p].[NullableString], [p].[NullableStrings], [p].[String], [p].[Strings]
559+
SELECT [p].[Id], [p].[Bool], [p].[Bools], [p].[DateTime], [p].[DateTimes], [p].[Enum], [p].[Enums], [p].[Int], [p].[Ints], [p].[NullableInt], [p].[NullableInts], [p].[NullableString], [p].[NullableStrings], [p].[NullableWrappedId], [p].[NullableWrappedIdWithNullableComparer], [p].[String], [p].[Strings], [p].[WrappedId]
560560
FROM [PrimitiveCollectionsEntity] AS [p]
561561
WHERE [p].[Int] IN (
562562
SELECT [i].[value]
@@ -567,7 +567,7 @@ FROM OPENJSON(@ints) WITH ([value] int '$') AS [i]
567567
"""
568568
@ints='[10,999]' (Nullable = false) (Size = 4000)
569569
570-
SELECT [p].[Id], [p].[Bool], [p].[Bools], [p].[DateTime], [p].[DateTimes], [p].[Enum], [p].[Enums], [p].[Int], [p].[Ints], [p].[NullableInt], [p].[NullableInts], [p].[NullableString], [p].[NullableStrings], [p].[String], [p].[Strings]
570+
SELECT [p].[Id], [p].[Bool], [p].[Bools], [p].[DateTime], [p].[DateTimes], [p].[Enum], [p].[Enums], [p].[Int], [p].[Ints], [p].[NullableInt], [p].[NullableInts], [p].[NullableString], [p].[NullableStrings], [p].[NullableWrappedId], [p].[NullableWrappedIdWithNullableComparer], [p].[String], [p].[Strings], [p].[WrappedId]
571571
FROM [PrimitiveCollectionsEntity] AS [p]
572572
WHERE [p].[Int] NOT IN (
573573
SELECT [i].[value]

test/EFCore.Sqlite.FunctionalTests/Query/PrimitiveCollectionsQuerySqliteTest.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ public override async Task Parameter_collection_ImmutableArray_of_ints_Contains_
546546
"""
547547
@ints='[10,999]' (Nullable = false) (Size = 8)
548548
549-
SELECT "p"."Id", "p"."Bool", "p"."Bools", "p"."DateTime", "p"."DateTimes", "p"."Enum", "p"."Enums", "p"."Int", "p"."Ints", "p"."NullableInt", "p"."NullableInts", "p"."NullableString", "p"."NullableStrings", "p"."String", "p"."Strings"
549+
SELECT "p"."Id", "p"."Bool", "p"."Bools", "p"."DateTime", "p"."DateTimes", "p"."Enum", "p"."Enums", "p"."Int", "p"."Ints", "p"."NullableInt", "p"."NullableInts", "p"."NullableString", "p"."NullableStrings", "p"."NullableWrappedId", "p"."NullableWrappedIdWithNullableComparer", "p"."String", "p"."Strings", "p"."WrappedId"
550550
FROM "PrimitiveCollectionsEntity" AS "p"
551551
WHERE "p"."Int" IN (
552552
SELECT "i"."value"
@@ -557,7 +557,7 @@ FROM json_each(@ints) AS "i"
557557
"""
558558
@ints='[10,999]' (Nullable = false) (Size = 8)
559559
560-
SELECT "p"."Id", "p"."Bool", "p"."Bools", "p"."DateTime", "p"."DateTimes", "p"."Enum", "p"."Enums", "p"."Int", "p"."Ints", "p"."NullableInt", "p"."NullableInts", "p"."NullableString", "p"."NullableStrings", "p"."String", "p"."Strings"
560+
SELECT "p"."Id", "p"."Bool", "p"."Bools", "p"."DateTime", "p"."DateTimes", "p"."Enum", "p"."Enums", "p"."Int", "p"."Ints", "p"."NullableInt", "p"."NullableInts", "p"."NullableString", "p"."NullableStrings", "p"."NullableWrappedId", "p"."NullableWrappedIdWithNullableComparer", "p"."String", "p"."Strings", "p"."WrappedId"
561561
FROM "PrimitiveCollectionsEntity" AS "p"
562562
WHERE "p"."Int" NOT IN (
563563
SELECT "i"."value"

0 commit comments

Comments
 (0)