From 151d4d9f204bcce6daabf9e01f0d3ba70ebfdb2f Mon Sep 17 00:00:00 2001 From: Shay Rojansky Date: Sat, 29 Jun 2019 00:35:10 +0200 Subject: [PATCH] Fix conditional expression handling entity equality visitor Fixes #16335 --- .../EntityEqualityRewritingExpressionVisitor.cs | 4 ++-- .../Query/SimpleQueryTestBase.Where.cs | 11 +++++++++++ .../Query/SimpleQuerySqlServerTest.Where.cs | 13 +++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/EFCore/Query/Pipeline/EntityEqualityRewritingExpressionVisitor.cs b/src/EFCore/Query/Pipeline/EntityEqualityRewritingExpressionVisitor.cs index 8e3ce5e8767..5892fcbd30f 100644 --- a/src/EFCore/Query/Pipeline/EntityEqualityRewritingExpressionVisitor.cs +++ b/src/EFCore/Query/Pipeline/EntityEqualityRewritingExpressionVisitor.cs @@ -94,7 +94,7 @@ protected override Expression VisitTypeBinary(TypeBinaryExpression typeBinaryExp { // This is for "x is y" var visitedExpression = Visit(typeBinaryExpression.Expression); - var visitedTypeBinary= typeBinaryExpression.Update(Unwrap(visitedExpression)); + var visitedTypeBinary = typeBinaryExpression.Update(Unwrap(visitedExpression)); return visitedExpression is EntityReferenceExpression entityWrapper ? entityWrapper.Update(visitedTypeBinary) : (Expression)visitedTypeBinary; @@ -106,7 +106,7 @@ protected override Expression VisitConditional(ConditionalExpression conditional var newIfTrue = Visit(conditionalExpression.IfTrue); var newIfFalse = Visit(conditionalExpression.IfFalse); - var newConditional = conditionalExpression.Update(newTest, Unwrap(newIfTrue), Unwrap(newIfFalse)); + var newConditional = conditionalExpression.Update(Unwrap(newTest), Unwrap(newIfTrue), Unwrap(newIfFalse)); // TODO: the true and false sides may refer different entity types which happen to have the same // CLR type (e.g. shared entities) diff --git a/test/EFCore.Specification.Tests/Query/SimpleQueryTestBase.Where.cs b/test/EFCore.Specification.Tests/Query/SimpleQueryTestBase.Where.cs index e155911037c..04420ac3667 100644 --- a/test/EFCore.Specification.Tests/Query/SimpleQueryTestBase.Where.cs +++ b/test/EFCore.Specification.Tests/Query/SimpleQueryTestBase.Where.cs @@ -2016,5 +2016,16 @@ public virtual Task Decimal_cast_to_double_works(bool isAsync) ps => ps.Where(p => (double?)p.UnitPrice > 100), entryCount: 2); } + + [ConditionalTheory] // issue #16335 + [MemberData(nameof(IsAsyncData))] + public virtual Task Where_is_conditional(bool isAsync) + { + var customer = new Customer(); + + return AssertQuery( + isAsync, + ps => ps.Where(p => p is Product ? false : true)); + } } } diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/SimpleQuerySqlServerTest.Where.cs b/test/EFCore.SqlServer.FunctionalTests/Query/SimpleQuerySqlServerTest.Where.cs index 040c2615405..40c6a0f8f1b 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/SimpleQuerySqlServerTest.Where.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/SimpleQuerySqlServerTest.Where.cs @@ -1772,5 +1772,18 @@ public override async Task TypeBinary_short_circuit(bool isAsync) FROM [Orders] AS [o] WHERE @__p_0 = CAST(1 AS bit)"); } + + public override async Task Where_is_conditional(bool isAsync) + { + await base.Where_is_conditional(isAsync); + + AssertSql( + @"SELECT [p].[ProductID], [p].[Discontinued], [p].[ProductName], [p].[SupplierID], [p].[UnitPrice], [p].[UnitsInStock] +FROM [Products] AS [p] +WHERE CASE + WHEN CAST(1 AS bit) = CAST(1 AS bit) THEN CAST(0 AS bit) + ELSE CAST(1 AS bit) +END = CAST(1 AS bit)"); + } } }