Skip to content

Commit 1c5cd69

Browse files
authored
Translate List<T>.Exists to Queryable.Any (#38226)
Rewrites List<T>.Exists(predicate) to AsQueryable().Any(predicate) in QueryableMethodNormalizingExpressionVisitor, mirroring the existing ICollection<T>.Contains conversion. The Predicate<T> lambda is rebuilt as Func<T, bool> so the resulting Queryable.Any call type-checks. Flips four placeholder tests (Where_Join_Exists, _Inequality, _Constant, Where_Join_Not_Exists) from AssertTranslationFailed to AssertQuery, and populates the SqlServer SQL baselines with the EXISTS subquery output. Fixes #17762
1 parent e4afabf commit 1c5cd69

4 files changed

Lines changed: 84 additions & 20 deletions

File tree

‎src/EFCore/Query/Internal/QueryableMethodNormalizingExpressionVisitor.cs‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,13 @@ protected override Expression VisitMethodCall(MethodCallExpression methodCallExp
178178
visitedExpression = TryConvertCollectionContainsToQueryableContains(methodCallExpression);
179179
}
180180

181+
if (method.Name == nameof(List<>.Exists)
182+
&& method.DeclaringType is { IsGenericType: true } existsDeclaringType
183+
&& existsDeclaringType.GetGenericTypeDefinition() == typeof(List<>))
184+
{
185+
visitedExpression = TryConvertListExistsToQueryableAny(methodCallExpression);
186+
}
187+
181188
if (method.DeclaringType == typeof(EntityFrameworkQueryableExtensions)
182189
&& method.Name is nameof(EntityFrameworkQueryableExtensions.Include)
183190
or nameof(EntityFrameworkQueryableExtensions.ThenInclude)
@@ -506,6 +513,35 @@ private Expression TryConvertEnumerableToQueryable(MethodCallExpression methodCa
506513
return methodCallExpression.Update(Visit(methodCallExpression.Object), arguments);
507514
}
508515

516+
private Expression TryConvertListExistsToQueryableAny(MethodCallExpression methodCallExpression)
517+
{
518+
if (methodCallExpression.Object is MemberInitExpression or NewExpression)
519+
{
520+
return base.VisitMethodCall(methodCallExpression);
521+
}
522+
523+
// List<T>.Exists takes a Predicate<T>; rewrite the lambda to Func<T, bool> so it matches
524+
// Queryable.Any's Expression<Func<T, bool>> parameter.
525+
if (methodCallExpression.Arguments[0] is not LambdaExpression predicateLambda)
526+
{
527+
return base.VisitMethodCall(methodCallExpression);
528+
}
529+
530+
var sourceType = methodCallExpression.Method.DeclaringType!.GetGenericArguments()[0];
531+
var rewrittenPredicate = Expression.Lambda(
532+
typeof(Func<,>).MakeGenericType(sourceType, typeof(bool)),
533+
predicateLambda.Body,
534+
predicateLambda.Parameters);
535+
536+
return VisitMethodCall(
537+
Expression.Call(
538+
QueryableMethods.AnyWithPredicate.MakeGenericMethod(sourceType),
539+
Expression.Call(
540+
QueryableMethods.AsQueryable.MakeGenericMethod(sourceType),
541+
methodCallExpression.Object!),
542+
Expression.Quote(rewrittenPredicate)));
543+
}
544+
509545
private Expression TryConvertCollectionContainsToQueryableContains(MethodCallExpression methodCallExpression)
510546
{
511547
if (methodCallExpression.Object is MemberInitExpression or NewExpression)

‎test/EFCore.Cosmos.FunctionalTests/Query/NorthwindMiscellaneousQueryCosmosTest.cs‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,28 +1094,32 @@ public override async Task Where_Join_Any(bool async)
10941094

10951095
public override async Task Where_Join_Exists(bool async)
10961096
{
1097-
await base.Where_Join_Exists(async);
1097+
// Cosmos client evaluation. Issue #17246.
1098+
await AssertTranslationFailed(() => base.Where_Join_Exists(async));
10981099

10991100
AssertSql();
11001101
}
11011102

11021103
public override async Task Where_Join_Exists_Inequality(bool async)
11031104
{
1104-
await base.Where_Join_Exists_Inequality(async);
1105+
// Cosmos client evaluation. Issue #17246.
1106+
await AssertTranslationFailed(() => base.Where_Join_Exists_Inequality(async));
11051107

11061108
AssertSql();
11071109
}
11081110

11091111
public override async Task Where_Join_Exists_Constant(bool async)
11101112
{
1111-
await base.Where_Join_Exists_Constant(async);
1113+
// Cosmos client evaluation. Issue #17246.
1114+
await AssertTranslationFailed(() => base.Where_Join_Exists_Constant(async));
11121115

11131116
AssertSql();
11141117
}
11151118

11161119
public override async Task Where_Join_Not_Exists(bool async)
11171120
{
1118-
await base.Where_Join_Not_Exists(async);
1121+
// Cosmos client evaluation. Issue #17246.
1122+
await AssertTranslationFailed(() => base.Where_Join_Not_Exists(async));
11191123

11201124
AssertSql();
11211125
}

‎test/EFCore.Specification.Tests/Query/NorthwindMiscellaneousQueryTestBase.cs‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,33 +1942,31 @@ public virtual Task Where_Join_Any(bool async)
19421942

19431943
[Theory, MemberData(nameof(IsAsyncData))]
19441944
public virtual Task Where_Join_Exists(bool async)
1945-
// Translate List.Exists. Issue #17762.
1946-
=> AssertTranslationFailed(() => AssertQuery(
1945+
=> AssertQuery(
19471946
async,
19481947
ss => ss.Set<Customer>()
1949-
.Where(c => c.CustomerID == "ALFKI" && c.Orders.Exists(o => o.OrderDate == new DateTime(2008, 10, 24)))));
1948+
.Where(c => c.CustomerID == "ALFKI" && c.Orders.Exists(o => o.OrderDate == new DateTime(2008, 10, 24))),
1949+
assertEmpty: true);
19501950

19511951
[Theory, MemberData(nameof(IsAsyncData))]
19521952
public virtual Task Where_Join_Exists_Inequality(bool async)
1953-
// Translate List.Exists. Issue #17762.
1954-
=> AssertTranslationFailed(() => AssertQuery(
1953+
=> AssertQuery(
19551954
async,
19561955
ss => ss.Set<Customer>()
1957-
.Where(c => c.CustomerID == "ALFKI" && c.Orders.Exists(o => o.OrderDate != new DateTime(2008, 10, 24)))));
1956+
.Where(c => c.CustomerID == "ALFKI" && c.Orders.Exists(o => o.OrderDate != new DateTime(2008, 10, 24))));
19581957

19591958
[Theory, MemberData(nameof(IsAsyncData))]
19601959
public virtual Task Where_Join_Exists_Constant(bool async)
1961-
// Translate List.Exists. Issue #17762.
1962-
=> AssertTranslationFailed(() => AssertQuery(
1960+
=> AssertQuery(
19631961
async,
1964-
ss => ss.Set<Customer>().Where(c => c.CustomerID == "ALFKI" && c.Orders.Exists(o => false))));
1962+
ss => ss.Set<Customer>().Where(c => c.CustomerID == "ALFKI" && c.Orders.Exists(o => false)),
1963+
assertEmpty: true);
19651964

19661965
[Theory, MemberData(nameof(IsAsyncData))]
19671966
public virtual Task Where_Join_Not_Exists(bool async)
1968-
// Translate List.Exists. Issue #17762.
1969-
=> AssertTranslationFailed(() => AssertQuery(
1967+
=> AssertQuery(
19701968
async,
1971-
ss => ss.Set<Customer>().Where(c => c.CustomerID == "ALFKI" && !c.Orders.Exists(o => false))));
1969+
ss => ss.Set<Customer>().Where(c => c.CustomerID == "ALFKI" && !c.Orders.Exists(o => false)));
19721970

19731971
[Theory, MemberData(nameof(IsAsyncData))]
19741972
public virtual Task Multiple_joins_Where_Order_Any(bool async)

‎test/EFCore.SqlServer.FunctionalTests/Query/NorthwindMiscellaneousQuerySqlServerTest.cs‎

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1964,28 +1964,54 @@ public override async Task Where_Join_Exists(bool async)
19641964
{
19651965
await base.Where_Join_Exists(async);
19661966

1967-
AssertSql();
1967+
AssertSql(
1968+
"""
1969+
SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region]
1970+
FROM [Customers] AS [c]
1971+
WHERE [c].[CustomerID] = N'ALFKI' AND EXISTS (
1972+
SELECT 1
1973+
FROM [Orders] AS [o]
1974+
WHERE [c].[CustomerID] = [o].[CustomerID] AND [o].[OrderDate] = '2008-10-24T00:00:00.000')
1975+
""");
19681976
}
19691977

19701978
public override async Task Where_Join_Exists_Inequality(bool async)
19711979
{
19721980
await base.Where_Join_Exists_Inequality(async);
19731981

1974-
AssertSql();
1982+
AssertSql(
1983+
"""
1984+
SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region]
1985+
FROM [Customers] AS [c]
1986+
WHERE [c].[CustomerID] = N'ALFKI' AND EXISTS (
1987+
SELECT 1
1988+
FROM [Orders] AS [o]
1989+
WHERE [c].[CustomerID] = [o].[CustomerID] AND ([o].[OrderDate] <> '2008-10-24T00:00:00.000' OR [o].[OrderDate] IS NULL))
1990+
""");
19751991
}
19761992

19771993
public override async Task Where_Join_Exists_Constant(bool async)
19781994
{
19791995
await base.Where_Join_Exists_Constant(async);
19801996

1981-
AssertSql();
1997+
AssertSql(
1998+
"""
1999+
SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region]
2000+
FROM [Customers] AS [c]
2001+
WHERE 0 = 1
2002+
""");
19822003
}
19832004

19842005
public override async Task Where_Join_Not_Exists(bool async)
19852006
{
19862007
await base.Where_Join_Not_Exists(async);
19872008

1988-
AssertSql();
2009+
AssertSql(
2010+
"""
2011+
SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region]
2012+
FROM [Customers] AS [c]
2013+
WHERE [c].[CustomerID] = N'ALFKI'
2014+
""");
19892015
}
19902016

19912017
public override async Task Join_OrderBy_Count(bool async)

0 commit comments

Comments
 (0)