diff --git a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/SelectExpression.cs b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/SelectExpression.cs index f849abfd410..55e95bcc5e6 100644 --- a/src/EFCore.Relational/Query/Pipeline/SqlExpressions/SelectExpression.cs +++ b/src/EFCore.Relational/Query/Pipeline/SqlExpressions/SelectExpression.cs @@ -1083,19 +1083,26 @@ private bool Equals(SelectExpression selectExpression) return false; } + if (SetOperationType != selectExpression.SetOperationType) + { + return false; + } + if (_projectionMapping.Count != selectExpression._projectionMapping.Count) { - foreach (var projectionMapping in _projectionMapping) + return false; + } + + foreach (var projectionMapping in _projectionMapping) + { + if (!selectExpression._projectionMapping.TryGetValue(projectionMapping.Key, out var projection)) { - if (!selectExpression._projectionMapping.TryGetValue(projectionMapping.Key, out var projection)) - { - return false; - } + return false; + } - if (!projectionMapping.Value.Equals(projection)) - { - return false; - } + if (!projectionMapping.Value.Equals(projection)) + { + return false; } } @@ -1167,6 +1174,9 @@ public override int GetHashCode() { var hash = new HashCode(); hash.Add(base.GetHashCode()); + + hash.Add(SetOperationType); + foreach (var projectionMapping in _projectionMapping) { hash.Add(projectionMapping.Key); @@ -1206,47 +1216,59 @@ public override void Print(ExpressionPrinter expressionPrinter) } expressionPrinter.StringBuilder.AppendLine(); - if (Alias != null) + + if (IsSetOperation) { - expressionPrinter.StringBuilder.AppendLine("("); - expressionPrinter.StringBuilder.IncrementIndent(); + expressionPrinter.Visit(Tables[0]); + expressionPrinter.StringBuilder + .AppendLine() + .AppendLine(SetOperationType.ToString().ToUpperInvariant()); + expressionPrinter.Visit(Tables[1]); } + else + { + if (Alias != null) + { + expressionPrinter.StringBuilder.AppendLine("("); + expressionPrinter.StringBuilder.IncrementIndent(); + } - expressionPrinter.StringBuilder.Append("SELECT "); + expressionPrinter.StringBuilder.Append("SELECT "); - if (IsDistinct) - { - expressionPrinter.StringBuilder.Append("DISTINCT "); - } + if (IsDistinct) + { + expressionPrinter.StringBuilder.Append("DISTINCT "); + } - if (Limit != null - && Offset == null) - { - expressionPrinter.StringBuilder.Append("TOP("); - expressionPrinter.Visit(Limit); - expressionPrinter.StringBuilder.Append(") "); - } + if (Limit != null + && Offset == null) + { + expressionPrinter.StringBuilder.Append("TOP("); + expressionPrinter.Visit(Limit); + expressionPrinter.StringBuilder.Append(") "); + } - if (Projection.Any()) - { - expressionPrinter.VisitList(Projection); - } - else - { - expressionPrinter.StringBuilder.Append("1"); - } + if (Projection.Any()) + { + expressionPrinter.VisitList(Projection); + } + else + { + expressionPrinter.StringBuilder.Append("1"); + } - if (Tables.Any()) - { - expressionPrinter.StringBuilder.AppendLine().Append("FROM "); + if (Tables.Any()) + { + expressionPrinter.StringBuilder.AppendLine().Append("FROM "); - expressionPrinter.VisitList(Tables, p => p.StringBuilder.AppendLine()); - } + expressionPrinter.VisitList(Tables, p => p.StringBuilder.AppendLine()); + } - if (Predicate != null) - { - expressionPrinter.StringBuilder.AppendLine().Append("WHERE "); - expressionPrinter.Visit(Predicate); + if (Predicate != null) + { + expressionPrinter.StringBuilder.AppendLine().Append("WHERE "); + expressionPrinter.Visit(Predicate); + } } if (Orderings.Any())