diff --git a/src/EFCore.Relational/Query/SqlNullabilityProcessor.cs b/src/EFCore.Relational/Query/SqlNullabilityProcessor.cs index 8cb1157cfd7..f5520e3a431 100644 --- a/src/EFCore.Relational/Query/SqlNullabilityProcessor.cs +++ b/src/EFCore.Relational/Query/SqlNullabilityProcessor.cs @@ -551,10 +551,18 @@ protected virtual SqlExpression VisitExists( var subquery = Visit(existsExpression.Subquery); nullable = false; + if (AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore23617", out var enabled) && enabled) + { + return TryGetBoolConstantValue(subquery.Predicate) == false + ? subquery.Predicate + : existsExpression.Update(subquery); + } + // if subquery has predicate which evaluates to false, we can simply return false + // if the exisits is negated we need to return true instead return TryGetBoolConstantValue(subquery.Predicate) == false - ? subquery.Predicate - : existsExpression.Update(subquery); + ? _sqlExpressionFactory.Constant(existsExpression.IsNegated, existsExpression.TypeMapping) + : (SqlExpression)existsExpression.Update(subquery); } /// diff --git a/test/EFCore.Cosmos.FunctionalTests/Query/NorthwindAggregateOperatorsQueryCosmosTest.cs b/test/EFCore.Cosmos.FunctionalTests/Query/NorthwindAggregateOperatorsQueryCosmosTest.cs index fd58a709d54..f9ac2fed316 100644 --- a/test/EFCore.Cosmos.FunctionalTests/Query/NorthwindAggregateOperatorsQueryCosmosTest.cs +++ b/test/EFCore.Cosmos.FunctionalTests/Query/NorthwindAggregateOperatorsQueryCosmosTest.cs @@ -1620,6 +1620,18 @@ public override Task Multiple_collection_navigation_with_FirstOrDefault_chained( return base.Multiple_collection_navigation_with_FirstOrDefault_chained(async); } + [ConditionalTheory(Skip = "Issue#20441")] + public override Task All_true(bool async) + { + return base.All_true(async); + } + + [ConditionalTheory(Skip = "Issue#20441")] + public override Task Not_Any_false(bool async) + { + return base.Not_Any_false(async); + } + private void AssertSql(params string[] expected) => Fixture.TestSqlLoggerFactory.AssertBaseline(expected); diff --git a/test/EFCore.Specification.Tests/Query/NorthwindAggregateOperatorsQueryTestBase.cs b/test/EFCore.Specification.Tests/Query/NorthwindAggregateOperatorsQueryTestBase.cs index d57d06760f9..94120a94244 100644 --- a/test/EFCore.Specification.Tests/Query/NorthwindAggregateOperatorsQueryTestBase.cs +++ b/test/EFCore.Specification.Tests/Query/NorthwindAggregateOperatorsQueryTestBase.cs @@ -1986,5 +1986,24 @@ public virtual Task Min_after_default_if_empty_does_not_throw(bool isAsync) isAsync, ss => ss.Set().Where(o => o.OrderID == 10243).Select(o => o.OrderID).DefaultIfEmpty()); } + + [ConditionalTheory] + [MemberData(nameof(IsAsyncData))] + public virtual Task All_true(bool async) + { + return AssertAll( + async, + ss => ss.Set(), + predicate: x => true); + } + + [ConditionalTheory] + [MemberData(nameof(IsAsyncData))] + public virtual Task Not_Any_false(bool async) + { + return AssertQuery( + async, + ss => ss.Set().Where(c => !c.Orders.Any(o => false)).Select(c => c.CustomerID)); + } } } diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindAggregateOperatorsQuerySqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindAggregateOperatorsQuerySqlServerTest.cs index d8b39f7d7f2..615ddd84bc3 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindAggregateOperatorsQuerySqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindAggregateOperatorsQuerySqlServerTest.cs @@ -1466,6 +1466,23 @@ public override async Task Count_on_projection_with_client_eval(bool async) FROM [Orders] AS [o]"); } + public override async Task All_true(bool async) + { + await base.All_true(async); + + AssertSql( + @"SELECT CAST(1 AS bit)"); + } + + public override async Task Not_Any_false(bool async) + { + await base.Not_Any_false(async); + + AssertSql( + @"SELECT [c].[CustomerID] +FROM [Customers] AS [c]"); + } + private void AssertSql(params string[] expected) => Fixture.TestSqlLoggerFactory.AssertBaseline(expected);