Skip to content

Split query + projection with Skip/Take: paging subquery still has no key ordering in EF 10, child collections come back empty #39206

Closed

Description

@wpsmaxi

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]: False

Stack 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

  1. added theissue type on Oct 10, 2026
  2. removed theissue type on Oct 10, 2026
  3. added theissue type on Oct 10, 2026
  4. AndriySvyryd commented on Oct 10, 2026

    @AndriySvyryd
    Member

    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 when Name is not unique, leaving projected child collections empty.

    On main, the collection query pages before the key ordering:

    SELECT [c1].[Id], [p0].[Id]
    FROM (
        SELECT [p].[Id], [p].[Name]
        FROM [Parents] AS [p]
        ORDER BY [p].[Name]
        OFFSET @p ROWS FETCH NEXT @p1 ROWS ONLY
    ) AS [p0]
    INNER JOIN [Child] AS [c1] ON [p0].[Id] = [c1].[ParentId]
    ORDER BY [p0].[Name], [p0].[Id]

    Root-cause direction

    The relevant logic is in SelectExpression.ApplyProjection and its split-collection handling:

    1. A single-result/reference projection combined with paging causes the outer select to be pushed down early.
    2. The split-query fix from Ensure that split queries with non-deterministic Skip/Take don't produce wrong results (by adding deterministic orderings) #26808 later appends identifier orderings to the current select and its clone.
    3. At that point, the paging operation is already inside a subquery, so appending the identifiers affects only the outer select. It does not alter the inner ordering that controls 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, and Skip/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
    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)
        .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 optionsBuilder)
            => optionsBuilder.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; }
    }
  5. added this to the Backlog milestone on Oct 10, 2026
  6. sk-20231 commented on Nov 11, 2026

    @sk-20231
    Contributor

    I reproduced this on 10.0.10 and on 11.0.0 (SQL Server provider, ToQueryString(), no database needed). The same gap also affects Include when a reference navigation is included, not only projections:

    Query (all with AsSplitQuery(), OrderBy(Name), Skip(0).Take(10)) Key in the paging ORDER BY (10.0.10 and 11.0.0)
    Projection with p.Location.Name and p.Children.Select(c => c.Id) (this issue) No: ORDER BY [p].[Name]
    Same projection without p.Location.Name Yes: ORDER BY [p].[Name], [p].[Id]
    Include(p => p.Location).Include(p => p.Children) No: ORDER BY [p].[Name]
    Include(p => p.Children) only Yes: ORDER 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 the Include case, the generated SQL is:

    SELECT [p0].[Id], [p0].[LocationId], [p0].[Name], [l].[Id], [l].[Name]
    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]

    The model is the one from the issue description. The Include query is:

    db.Parents.AsSplitQuery()
        .Include(p => p.Location)
        .Include(p => p.Children)
        .OrderBy(p => p.Name)
        .Skip(0).Take(10)
        .ToQueryString();

    I have not tested main yet.

    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/Offset after the pushdown. I'd add tests for both the projection and the Include shapes. 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.

  7. AndriySvyryd commented on Nov 14, 2026

    @AndriySvyryd
    Member

    @sk-20231 Go ahead

  8. added a commit that references this issue on Nov 17, 2026
    69af746
  9. SimonCropp commented on Nov 30, 2026

    @SimonCropp
    Contributor

    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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions