diff --git a/test/EFCore.Specification.Tests/Query/NorthwindJoinQueryTestBase.cs b/test/EFCore.Specification.Tests/Query/NorthwindJoinQueryTestBase.cs index 27590388b8f..25e8f118754 100644 --- a/test/EFCore.Specification.Tests/Query/NorthwindJoinQueryTestBase.cs +++ b/test/EFCore.Specification.Tests/Query/NorthwindJoinQueryTestBase.cs @@ -969,5 +969,23 @@ public virtual Task Take_SelectMany_correlated_subquery_take(bool async) .Take(2)), entryCount: 2); } + + [ConditionalTheory] + [MemberData(nameof(IsAsyncData))] + public virtual Task Take_in_collection_projection_with_FirstOrDefault_on_top_level(bool async) + { + return AssertFirstOrDefault( + async, + ss => ss.Set() + .Select(c => new + { + Orders = c.Orders.OrderBy(e => e.OrderDate).Take(1) + .Select(o => new + { + Title = o.CustomerID == o.Customer.City ? "A" : "B" + }).ToList() + }), + asserter: (e, a) => AssertCollection(e.Orders, a.Orders, ordered: true)); + } } } diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindJoinQuerySqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindJoinQuerySqlServerTest.cs index 51992e74621..3002fa70e27 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindJoinQuerySqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindJoinQuerySqlServerTest.cs @@ -722,6 +722,32 @@ FROM [Customers] AS [c0] ORDER BY [t].[CustomerID]"); } + public override async Task Take_in_collection_projection_with_FirstOrDefault_on_top_level(bool async) + { + await base.Take_in_collection_projection_with_FirstOrDefault_on_top_level(async); + + AssertSql( + @"SELECT [t].[CustomerID], [t0].[Title], [t0].[OrderID], [t0].[CustomerID] +FROM ( + SELECT TOP(1) [c].[CustomerID] + FROM [Customers] AS [c] +) AS [t] +OUTER APPLY ( + SELECT CASE + WHEN ([t1].[CustomerID] = [c0].[City]) OR ([t1].[CustomerID] IS NULL AND [c0].[City] IS NULL) THEN N'A' + ELSE N'B' + END AS [Title], [t1].[OrderID], [c0].[CustomerID], [t1].[OrderDate] + FROM ( + SELECT TOP(1) [o].[OrderID], [o].[CustomerID], [o].[OrderDate] + FROM [Orders] AS [o] + WHERE [t].[CustomerID] = [o].[CustomerID] + ORDER BY [o].[OrderDate] + ) AS [t1] + LEFT JOIN [Customers] AS [c0] ON [t1].[CustomerID] = [c0].[CustomerID] +) AS [t0] +ORDER BY [t].[CustomerID], [t0].[OrderDate], [t0].[OrderID], [t0].[CustomerID]"); + } + private void AssertSql(params string[] expected) => Fixture.TestSqlLoggerFactory.AssertBaseline(expected); diff --git a/test/EFCore.Sqlite.FunctionalTests/Query/NorthwindJoinQuerySqliteTest.cs b/test/EFCore.Sqlite.FunctionalTests/Query/NorthwindJoinQuerySqliteTest.cs index 72697585bc4..f780672c32e 100644 --- a/test/EFCore.Sqlite.FunctionalTests/Query/NorthwindJoinQuerySqliteTest.cs +++ b/test/EFCore.Sqlite.FunctionalTests/Query/NorthwindJoinQuerySqliteTest.cs @@ -54,5 +54,11 @@ public override async Task SelectMany_with_selecting_outer_entity_column_and_inn SqliteStrings.ApplyNotSupported, (await Assert.ThrowsAsync( () => base.SelectMany_with_selecting_outer_entity_column_and_inner_column(async))).Message); + + public override async Task Take_in_collection_projection_with_FirstOrDefault_on_top_level(bool async) + => Assert.Equal( + SqliteStrings.ApplyNotSupported, + (await Assert.ThrowsAsync( + () => base.Take_in_collection_projection_with_FirstOrDefault_on_top_level(async))).Message); } }