diff --git a/src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs b/src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs index d3931c36b7b..977e5992da1 100644 --- a/src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs +++ b/src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs @@ -919,8 +919,8 @@ static Expression RemoveConvert(Expression expression) var actualParentIdentifier = _identifier.Take(outerSelectExpression._identifier.Count).ToList(); for (var j = 0; j < actualParentIdentifier.Count; j++) { - AppendOrdering(new OrderingExpression(actualParentIdentifier[j].Column, ascending: true)); - outerSelectExpression.AppendOrdering( + AppendOrderingInternal(new OrderingExpression(actualParentIdentifier[j].Column, ascending: true)); + outerSelectExpression.AppendOrderingInternal( new OrderingExpression(outerSelectExpression._identifier[j].Column, ascending: true)); } @@ -1859,6 +1859,11 @@ private static void PopulateGroupByTerms( /// An ordering expression to use for ordering. public void ApplyOrdering(OrderingExpression orderingExpression) { + if (Limit is SqlConstantExpression { Value: 1 }) + { + return; + } + if (IsDistinct || Limit != null || Offset != null) @@ -1876,14 +1881,22 @@ public void ApplyOrdering(OrderingExpression orderingExpression) /// An ordering expression to use for ordering. public void AppendOrdering(OrderingExpression orderingExpression) { - if (!_orderings.Any(o => o.Expression.Equals(orderingExpression.Expression))) + if (Limit is SqlConstantExpression { Value: 1 }) { - AppendOrderingInternal(orderingExpression); + return; } + + AppendOrderingInternal(orderingExpression); } private void AppendOrderingInternal(OrderingExpression orderingExpression) - => _orderings.Add(orderingExpression.Update(orderingExpression.Expression)); + { + if (!_orderings.Any(o => o.Expression.Equals(orderingExpression.Expression))) + { + _orderings.Add(orderingExpression); + } + } + /// /// Reverses the existing orderings on the . diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/ComplexNavigationsQuerySqlServer160Test.cs b/test/EFCore.SqlServer.FunctionalTests/Query/ComplexNavigationsQuerySqlServer160Test.cs index 5628b696ada..979075b673b 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/ComplexNavigationsQuerySqlServer160Test.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/ComplexNavigationsQuerySqlServer160Test.cs @@ -69,10 +69,9 @@ where l1.Id < 3 SELECT ( SELECT TOP(1) [l1].[Name] FROM ( - SELECT TOP(1) [l0].[Id], [l0].[Name] + SELECT TOP(1) [l0].[Name] FROM [LevelThree] AS [l0] - ) AS [l1] - ORDER BY [l1].[Id]) + ) AS [l1]) FROM [LevelOne] AS [l] WHERE [l].[Id] < 3 """); diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/ComplexNavigationsQuerySqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Query/ComplexNavigationsQuerySqlServerTest.cs index 4b9b1070b00..aed214ae688 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/ComplexNavigationsQuerySqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/ComplexNavigationsQuerySqlServerTest.cs @@ -69,10 +69,9 @@ where l1.Id < 3 SELECT ( SELECT TOP(1) [l1].[Name] FROM ( - SELECT TOP(1) [l0].[Id], [l0].[Name] + SELECT TOP(1) [l0].[Name] FROM [LevelThree] AS [l0] - ) AS [l1] - ORDER BY [l1].[Id]) + ) AS [l1]) FROM [LevelOne] AS [l] WHERE [l].[Id] < 3 """); diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/ComplexNavigationsSharedTypeQuerySqlServer160Test.cs b/test/EFCore.SqlServer.FunctionalTests/Query/ComplexNavigationsSharedTypeQuerySqlServer160Test.cs index 3ea3c2abe3d..c0f3bc093ba 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/ComplexNavigationsSharedTypeQuerySqlServer160Test.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/ComplexNavigationsSharedTypeQuerySqlServer160Test.cs @@ -87,7 +87,7 @@ where l1.Id < 3 SELECT ( SELECT TOP(1) [s].[Level3_Name] FROM ( - SELECT TOP(1) [l4].[Id], [l4].[Level2_Required_Id], [l4].[Level3_Name], [l4].[OneToMany_Required_Inverse3Id] + SELECT TOP(1) [l4].[Level3_Name] FROM [Level1] AS [l0] LEFT JOIN ( SELECT [l1].[Id], [l1].[OneToOne_Required_PK_Date], [l1].[Level1_Required_Id], [l1].[OneToMany_Required_Inverse2Id] @@ -106,10 +106,7 @@ WHEN [l2].[OneToOne_Required_PK_Date] IS NOT NULL AND [l2].[Level1_Required_Id] WHEN [l4].[Level2_Required_Id] IS NOT NULL AND [l4].[OneToMany_Required_Inverse3Id] IS NOT NULL THEN [l4].[Id] END WHERE [l2].[OneToOne_Required_PK_Date] IS NOT NULL AND [l2].[Level1_Required_Id] IS NOT NULL AND [l2].[OneToMany_Required_Inverse2Id] IS NOT NULL AND [l4].[Level2_Required_Id] IS NOT NULL AND [l4].[OneToMany_Required_Inverse3Id] IS NOT NULL - ) AS [s] - ORDER BY CASE - WHEN [s].[Level2_Required_Id] IS NOT NULL AND [s].[OneToMany_Required_Inverse3Id] IS NOT NULL THEN [s].[Id] - END) + ) AS [s]) FROM [Level1] AS [l] WHERE [l].[Id] < 3 """); diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/ComplexNavigationsSharedTypeQuerySqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Query/ComplexNavigationsSharedTypeQuerySqlServerTest.cs index 00aba8b691e..a39f5cd27b2 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/ComplexNavigationsSharedTypeQuerySqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/ComplexNavigationsSharedTypeQuerySqlServerTest.cs @@ -89,7 +89,7 @@ where l1.Id < 3 SELECT ( SELECT TOP(1) [s].[Level3_Name] FROM ( - SELECT TOP(1) [l4].[Id], [l4].[Level2_Required_Id], [l4].[Level3_Name], [l4].[OneToMany_Required_Inverse3Id] + SELECT TOP(1) [l4].[Level3_Name] FROM [Level1] AS [l0] LEFT JOIN ( SELECT [l1].[Id], [l1].[OneToOne_Required_PK_Date], [l1].[Level1_Required_Id], [l1].[OneToMany_Required_Inverse2Id] @@ -108,10 +108,7 @@ WHEN [l2].[OneToOne_Required_PK_Date] IS NOT NULL AND [l2].[Level1_Required_Id] WHEN [l4].[Level2_Required_Id] IS NOT NULL AND [l4].[OneToMany_Required_Inverse3Id] IS NOT NULL THEN [l4].[Id] END WHERE [l2].[OneToOne_Required_PK_Date] IS NOT NULL AND [l2].[Level1_Required_Id] IS NOT NULL AND [l2].[OneToMany_Required_Inverse2Id] IS NOT NULL AND [l4].[Level2_Required_Id] IS NOT NULL AND [l4].[OneToMany_Required_Inverse3Id] IS NOT NULL - ) AS [s] - ORDER BY CASE - WHEN [s].[Level2_Required_Id] IS NOT NULL AND [s].[OneToMany_Required_Inverse3Id] IS NOT NULL THEN [s].[Id] - END) + ) AS [s]) FROM [Level1] AS [l] WHERE [l].[Id] < 3 """);