Correlated subquery (N+1 fix) missing ORDER BY clause inside nested query #11163
Labels
closed-fixed
The issue has been fixed and is/will be included in the release indicated by the issue milestone.
type-bug
Milestone
I am trying to do a simple query to get a list of
Club
rows with associated customerOrder
rows. (This is for a subscription model where a customer has aClub
membership).With the new EFCore 2.1 functionality only two queries are generated to avoid N+1 issues. I get back the correct results, until I add
OrderByDescending
in which case no data is returned for the 'child' query.It is clear what is happening by looking at the generated SQL. The first query here gets the top
@_p_0
clubs, and the second query gets the corresponding orders.This is great SQL except for one thing! The
ORDER BY [t].[NextShipmentDt] DESC
should be INSIDE the INNER query because it usesSELECT TOP
. This results in completely the wrong orders being returned for the club memberships. Then EFCore fails to match them up and returns me zero results.Expected correct SQL (one line added)
Steps to reproduce
The code is very simple, uses
ToList()
to force the new behavior as described in the release notes. I was able to repeat this result with any query containingOrderByDescending
.(This code differs slightly from the code to generate the SQL, but the structure is the same).
OrderNavigation
is defined in theClub
entity as:with the following constraint:
Further technical details
EF Core version: Entity Framework Core 2.1.0-preview1-28290
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Win 10
IDE: Visual Studio 2017 15.6.0 Preview 7
The text was updated successfully, but these errors were encountered: