Skip to content
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
14 changes: 9 additions & 5 deletions src/EFCore/Query/QueryRootProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,22 @@ protected override Expression VisitMethodCall(MethodCallExpression methodCallExp

private Expression VisitQueryRootCandidate(Expression expression, Type elementClrType)
{
var candidateExpression = expression;

// A:
// In case the collection was value type, in order to call methods like AsQueryable,
// we need to convert it to IEnumerable<T> which requires boxing.
// We do that with Convert expression which we need to unwrap here.
//
// B:
// For collections that are abstract (i.e. FrozenSet<T>) the compiler uses some "internal"
// implementation and for readonly fields the compiler adds explicit cast. We need to
// unwrap here.
Comment on lines +92 to +94
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// For collections that are abstract (i.e. FrozenSet<T>) the compiler uses some "internal"
// implementation and for readonly fields the compiler adds explicit cast. We need to
// unwrap here.
// For collections that are abstract (i.e. FrozenSet<T>), some internal type is used
// and for readonly fields the compiler adds explicit cast. We need to unwrap here.

if (expression is UnaryExpression { NodeType: ExpressionType.Convert } convertExpression
&& convertExpression.Type.GetGenericTypeDefinition() == typeof(IEnumerable<>))
&& convertExpression.Type.GetGenericTypeImplementations(typeof(IEnumerable<>)) is not null)
Copy link
Member

Choose a reason for hiding this comment

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

Given that below we match on non-generic IEnumerable, should we match for this here as well instead of the generic variant?

{
candidateExpression = convertExpression.Operand;
return VisitQueryRootCandidate(convertExpression.Operand, elementClrType);
}

switch (candidateExpression)
switch (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
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,28 @@ WHERE ARRAY_CONTAINS(@ints, c["Int"])
"""
@ints='[10,999]'

SELECT VALUE c
FROM root c
WHERE NOT(ARRAY_CONTAINS(@ints, c["Int"]))
""");
}

public override async Task Parameter_collection_FrozenSet_of_ints_Contains_int()
{
await base.Parameter_collection_FrozenSet_of_ints_Contains_int();

AssertSql(
"""
@ints='[10,999]'

SELECT VALUE c
FROM root c
WHERE ARRAY_CONTAINS(@ints, c["Int"])
""",
//
"""
@ints='[10,999]'

SELECT VALUE c
FROM root c
WHERE NOT(ARRAY_CONTAINS(@ints, c["Int"]))
Expand Down Expand Up @@ -807,6 +829,60 @@ public override Task Parameter_collection_Count_with_huge_number_of_values()
public override Task Parameter_collection_of_ints_Contains_int_with_huge_number_of_values()
=> base.Parameter_collection_of_ints_Contains_int_with_huge_number_of_values();

public override async Task Static_readonly_collection_List_of_ints_Contains_int()
{
await base.Static_readonly_collection_List_of_ints_Contains_int();

AssertSql(
"""
SELECT VALUE c
FROM root c
WHERE c["Int"] IN (10, 999)
""",
//
"""
SELECT VALUE c
FROM root c
WHERE c["Int"] NOT IN (10, 999)
""");
}

public override async Task Static_readonly_collection_FrozenSet_of_ints_Contains_int()
{
await base.Static_readonly_collection_FrozenSet_of_ints_Contains_int();

AssertSql(
"""
SELECT VALUE c
FROM root c
WHERE c["Int"] IN (10, 999)
""",
//
"""
SELECT VALUE c
FROM root c
WHERE c["Int"] NOT IN (10, 999)
""");
}

public override async Task Static_readonly_collection_ImmutableArray_of_ints_Contains_int()
{
await base.Static_readonly_collection_ImmutableArray_of_ints_Contains_int();

AssertSql(
"""
SELECT VALUE c
FROM root c
WHERE c["Int"] IN (10, 999)
""",
//
"""
SELECT VALUE c
FROM root c
WHERE c["Int"] NOT IN (10, 999)
""");
}

public override async Task Column_collection_of_ints_Contains()
{
await base.Column_collection_of_ints_Contains();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Frozen;
using System.Collections.Immutable;

namespace Microsoft.EntityFrameworkCore.Query;

public abstract class PrimitiveCollectionsQueryTestBase<TFixture>(TFixture fixture) : QueryTestBase<TFixture>(fixture)
where TFixture : PrimitiveCollectionsQueryTestBase<TFixture>.PrimitiveCollectionsQueryFixtureBase, new()
{
// List<T> is a regular class
Copy link
Member

Choose a reason for hiding this comment

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

👍

protected static readonly List<int> StaticReadonlyList = [10, 999];
// FrozenSet<T> is an abstract class
protected static readonly FrozenSet<int> StaticReadonlyFrozenSet = [10, 999];
// ImmutableArray<T> is a struct
protected static readonly ImmutableArray<int> StaticReadonlyImmutableArray = [10, 999];

public virtual int? NumberOfValuesForHugeParameterCollectionTests { get; } = null;

[ConditionalFact]
Expand Down Expand Up @@ -258,10 +266,19 @@ public virtual async Task Parameter_collection_HashSet_of_ints_Contains_int()
await AssertQuery(ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => !ints.Contains(c.Int)));
}

[ConditionalFact]
public virtual async Task Parameter_collection_FrozenSet_of_ints_Contains_int()
{
var ints = FrozenSet.Create(10, 999);

await AssertQuery(ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => ints.Contains(c.Int)));
await AssertQuery(ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => !ints.Contains(c.Int)));
}

[ConditionalFact]
public virtual async Task Parameter_collection_ImmutableArray_of_ints_Contains_int()
{
var ints = ImmutableArray.Create([10, 999]);
var ints = ImmutableArray.Create(10, 999);

await AssertQuery(ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => ints.Contains(c.Int)));
await AssertQuery(ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => !ints.Contains(c.Int)));
Expand Down Expand Up @@ -517,6 +534,27 @@ public virtual async Task Parameter_collection_of_ints_Contains_int_with_huge_nu
await AssertQuery(ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => !ints.Contains(c.Int)));
}

[ConditionalFact]
public virtual async Task Static_readonly_collection_List_of_ints_Contains_int()
{
await AssertQuery(ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => StaticReadonlyList.Contains(c.Int)));
await AssertQuery(ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => !StaticReadonlyList.Contains(c.Int)));
}

[ConditionalFact]
public virtual async Task Static_readonly_collection_FrozenSet_of_ints_Contains_int()
{
await AssertQuery(ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => StaticReadonlyFrozenSet.Contains(c.Int)));
await AssertQuery(ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => !StaticReadonlyFrozenSet.Contains(c.Int)));
}

[ConditionalFact]
public virtual async Task Static_readonly_collection_ImmutableArray_of_ints_Contains_int()
{
await AssertQuery(ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => StaticReadonlyImmutableArray.Contains(c.Int)));
await AssertQuery(ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => !StaticReadonlyImmutableArray.Contains(c.Int)));
}

[ConditionalFact]
public virtual Task Column_collection_of_ints_Contains()
=> AssertQuery(ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => c.Ints.Contains(10)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,30 @@ WHERE [p].[Int] IN (@ints1, @ints2)
@ints1='10'
@ints2='999'

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]
FROM [PrimitiveCollectionsEntity] AS [p]
WHERE [p].[Int] NOT IN (@ints1, @ints2)
""");
}

public override async Task Parameter_collection_FrozenSet_of_ints_Contains_int()
{
await base.Parameter_collection_FrozenSet_of_ints_Contains_int();

AssertSql(
"""
@ints1='10'
@ints2='999'

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]
FROM [PrimitiveCollectionsEntity] AS [p]
WHERE [p].[Int] IN (@ints1, @ints2)
""",
//
"""
@ints1='10'
@ints2='999'

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]
FROM [PrimitiveCollectionsEntity] AS [p]
WHERE [p].[Int] NOT IN (@ints1, @ints2)
Expand Down Expand Up @@ -825,6 +849,60 @@ public override async Task Parameter_collection_of_ints_Contains_int_with_huge_n
Assert.DoesNotContain("OPENJSON", Fixture.TestSqlLoggerFactory.SqlStatements[1], StringComparison.Ordinal);
}

public override async Task Static_readonly_collection_List_of_ints_Contains_int()
{
await base.Static_readonly_collection_List_of_ints_Contains_int();

AssertSql(
"""
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]
FROM [PrimitiveCollectionsEntity] AS [p]
WHERE [p].[Int] IN (10, 999)
""",
//
"""
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]
FROM [PrimitiveCollectionsEntity] AS [p]
WHERE [p].[Int] NOT IN (10, 999)
""");
}

public override async Task Static_readonly_collection_FrozenSet_of_ints_Contains_int()
{
await base.Static_readonly_collection_FrozenSet_of_ints_Contains_int();

AssertSql(
"""
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]
FROM [PrimitiveCollectionsEntity] AS [p]
WHERE [p].[Int] IN (10, 999)
""",
//
"""
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]
FROM [PrimitiveCollectionsEntity] AS [p]
WHERE [p].[Int] NOT IN (10, 999)
""");
}

public override async Task Static_readonly_collection_ImmutableArray_of_ints_Contains_int()
{
await base.Static_readonly_collection_ImmutableArray_of_ints_Contains_int();

AssertSql(
"""
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]
FROM [PrimitiveCollectionsEntity] AS [p]
WHERE [p].[Int] IN (10, 999)
""",
//
"""
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]
FROM [PrimitiveCollectionsEntity] AS [p]
WHERE [p].[Int] NOT IN (10, 999)
""");
}

public override Task Column_collection_of_ints_Contains()
=> AssertCompatibilityLevelTooLow(() => base.Column_collection_of_ints_Contains());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,30 @@ WHERE [p].[Int] IN (@ints1, @ints2)
@ints1='10'
@ints2='999'

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]
FROM [PrimitiveCollectionsEntity] AS [p]
WHERE [p].[Int] NOT IN (@ints1, @ints2)
""");
}

public override async Task Parameter_collection_FrozenSet_of_ints_Contains_int()
{
await base.Parameter_collection_FrozenSet_of_ints_Contains_int();

AssertSql(
"""
@ints1='10'
@ints2='999'

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]
FROM [PrimitiveCollectionsEntity] AS [p]
WHERE [p].[Int] IN (@ints1, @ints2)
""",
//
"""
@ints1='10'
@ints2='999'

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]
FROM [PrimitiveCollectionsEntity] AS [p]
WHERE [p].[Int] NOT IN (@ints1, @ints2)
Expand Down Expand Up @@ -819,6 +843,60 @@ public override async Task Parameter_collection_of_ints_Contains_int_with_huge_n
Assert.Contains("OPENJSON(@ints) WITH ([value] int '$')", Fixture.TestSqlLoggerFactory.SqlStatements[1], StringComparison.Ordinal);
}

public override async Task Static_readonly_collection_List_of_ints_Contains_int()
{
await base.Static_readonly_collection_List_of_ints_Contains_int();

AssertSql(
"""
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]
FROM [PrimitiveCollectionsEntity] AS [p]
WHERE [p].[Int] IN (10, 999)
""",
//
"""
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]
FROM [PrimitiveCollectionsEntity] AS [p]
WHERE [p].[Int] NOT IN (10, 999)
""");
}

public override async Task Static_readonly_collection_FrozenSet_of_ints_Contains_int()
{
await base.Static_readonly_collection_FrozenSet_of_ints_Contains_int();

AssertSql(
"""
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]
FROM [PrimitiveCollectionsEntity] AS [p]
WHERE [p].[Int] IN (10, 999)
""",
//
"""
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]
FROM [PrimitiveCollectionsEntity] AS [p]
WHERE [p].[Int] NOT IN (10, 999)
""");
}

public override async Task Static_readonly_collection_ImmutableArray_of_ints_Contains_int()
{
await base.Static_readonly_collection_ImmutableArray_of_ints_Contains_int();

AssertSql(
"""
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]
FROM [PrimitiveCollectionsEntity] AS [p]
WHERE [p].[Int] IN (10, 999)
""",
//
"""
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]
FROM [PrimitiveCollectionsEntity] AS [p]
WHERE [p].[Int] NOT IN (10, 999)
""");
}

public override async Task Column_collection_of_ints_Contains()
{
await base.Column_collection_of_ints_Contains();
Expand Down
Loading
Loading