Repository navigation
Description
Bug description
A projected query runs as a split query with OrderBy + Skip/Take, and the ordering is not unique. On EF Core 10.0.10, some rows come back with empty child collections, even though the database has rows for them.
As far as I understand, #26808 (fixed in 10.0.0, "More consistent ordering for split queries") was meant to prevent exactly this by adding the key to the ordering inside the paging subquery.
It happens when the projection includes a reference navigation (p.Location.Name below): EF then pages inside a subquery, and that subquery's ORDER BY has no key. The key only appears in the outer ORDER BY, after OFFSET … FETCH.
Expected: with the #26808 fix, I expected EF to add the key ([p].[Id]) to the ordering inside the paging subquery here too, like it does for the Include example in the EF 10 release notes. If this shape is intentionally not covered, the docs ("prior to 10" in the split-query warning) currently suggest EF 10 users no longer need a unique ordering.
Your code
Working repo: https://github.com/wpsmaxi/split-query-empty-child-collections
using Microsoft.EntityFrameworkCore;
using var db = new Db();
var query = db.Parents
.AsSplitQuery()
.Select(p => new { p.Id, p.Name, LocationName = p.Location.Name, ChildIds = p.Children.Select(c => c.Id) })
.OrderBy(x => x.Name) // not unique
.Skip(0).Take(10);
var sql = query.ToQueryString();
var offset = sql.IndexOf("OFFSET");
var pagingOrderBy = sql[sql.LastIndexOf("ORDER BY", offset)..offset];
Console.WriteLine(sql);
Console.WriteLine();
Console.WriteLine($"Paging ORDER BY contains the key [p].[Id]: {pagingOrderBy.Contains("[p].[Id]")}");
class Db : DbContext
{
public DbSet<Parent> Parents => Set<Parent>();
protected override void OnConfiguring(DbContextOptionsBuilder o) => o.UseSqlServer("Server=.;Database=NotUsed");
}
class Parent
{
public int Id { get; set; }
public string Name { get; set; } = "";
public Location Location { get; set; } = null!;
public List<Child> Children { get; set; } = [];
}
class Location
{
public int Id { get; set; }
public string Name { get; set; } = "";
}
class Child
{
public int Id { get; set; }
}
Output (no database needed):
SELECT [p0].[Id], [p0].[Name], [l].[Name], [l].[Id]
FROM (
SELECT [p].[Id], [p].[LocationId], [p].[Name]
FROM [Parents] AS [p]
ORDER BY [p].[Name]
OFFSET @p ROWS FETCH NEXT @p1 ROWS ONLY
) AS [p0]
INNER JOIN [Location] AS [l] ON [p0].[LocationId] = [l].[Id]
ORDER BY [p0].[Name], [p0].[Id], [l].[Id]
Paging ORDER BY contains the key [p].[Id]: FalseStack traces
Verbose output
EF Core version
10.0.10
Database provider
Microsoft.EntityFrameworkCore.SqlServer 10.0.10
Target framework
.NET 10.0
Operating system
Windows 11 (local repro); production on Azure
IDE
Visual Studio 18.4.0
Activity
AndriySvyryd commentedon Oct 10, 2026
AI Triage
The below is an AI-generated analysis and may contain inaccuracies.
Result
The reported SQL-generation defect is confirmed on main.
The projection containing the reference navigation causes the root query to be pushed into a paging subquery before the split-query key ordering is appended. Consequently, the key is present only in the outer ordering and not in the ordering that determines which rows pass through
OFFSET/FETCH. The collection query repeats the same non-deterministic paging subquery. Separate executions can therefore select different parent rows whenNameis not unique, leaving projected child collections empty.On main, the collection query pages before the key ordering:
Root-cause direction
The relevant logic is in
SelectExpression.ApplyProjectionand its split-collection handling:OFFSET/FETCH.The fix should preserve the #26808 invariant when an earlier pushdown was caused by a reference/single-result projection: identifier orderings must reach the select containing
Limit/Offset, not only its outer wrapper. A regression test should cover a projected reference navigation plus a projected collection, non-unique ordering, andSkip/Take, and assert both SQL and materialized child collections.Duplicate search
No existing issue found in the search describes this EF 10 projection-with-reference-navigation gap as precisely as #39206.
Minimal repro
minimal repro
sk-20231 commentedon Nov 11, 2026
I reproduced this on 10.0.10 and on 11.0.0 (SQL Server provider,
ToQueryString(), no database needed). The same gap also affectsIncludewhen a reference navigation is included, not only projections:AsSplitQuery(),OrderBy(Name),Skip(0).Take(10))ORDER BY(10.0.10 and 11.0.0)p.Location.Nameandp.Children.Select(c => c.Id)(this issue)ORDER BY [p].[Name]p.Location.NameORDER BY [p].[Name], [p].[Id]Include(p => p.Location).Include(p => p.Children)ORDER BY [p].[Name]Include(p => p.Children)onlyORDER BY [p].[Name], [p].[Id]So the trigger seems to be the reference navigation, which pushes the paging into a subquery, whether it comes from a projection or from an
Include. For theIncludecase, the generated SQL is:The model is the one from the issue description. The
Includequery is:I have not tested
mainyet.I'd like to work on a fix, following the direction in the triage above: make sure the identifier orderings reach the select that holds
Limit/Offsetafter the pushdown. I'd add tests for both the projection and theIncludeshapes. Would a PR for this be welcome, or would you prefer to handle it within the team?Note
I used an AI assistant (Claude) to help investigate this and write this comment. The results come from a local repro that was run against the 10.0.10 and 11.0.0 packages.
AndriySvyryd commentedon Nov 14, 2026
@sk-20231 Go ahead
Fix split query paging order when limit/offset is pushed into a subqu…
Order the row limits of split queries by the key (#1508)
SimonCropp commentedon Nov 30, 2026
FWIW. it took me a significant amount of effort to work around this bug. And i still have to go back an undo it when the fix for this is deployed. given that, would it be possible to get this backported to 11.1.0?