Skip to content

Navigation access on the entity returned by GroupBy + First per group fails to translate ("Translation of member ... failed") #38686

Description

@ducmerida

Bug description

Accessing a navigation on the entity produced by a top-1-per-group query fails to translate, with a misleading "member is unmapped" message:

var query = context.Entries
    .GroupBy(e => e.RowNumber)
    .Select(g => g.OrderBy(x => x.Id).First())
    .OrderBy(e => e.ImportHistory.FileName);   // any navigation traversal here fails

The failing ingredient is precisely the navigation traversal after the per-group First():

  • GroupBy(key).Select(g => g.OrderBy(x => x.Id).First()) alone translates fine (ROW_NUMBER() OVER(PARTITION BY ...)).
  • A navigation member inside the group key also translates fine: GroupBy(e => new { e.RowNumber, e.ImportHistory.FileName }).Select(g => g.OrderBy(x => x.Id).First()) works, including the join.
  • Adding any navigation traversal after the First() — an OrderBy, a Where or a projection member — fails as above, even though the same traversal on a plain DbSet query translates normally.

It looks like the entity coming out of the grouping element subquery loses its ability to expand navigations (the "unmapped" wording is misleading — the member is mapped).

Related issues, for triage context: #26748 fixed operations on scalar members after GroupBy().Select(g => g.First()) in 11.0 — this report is the navigation-traversal case that remains broken on main after that fix. #28125 (join after per-group FirstOrDefault, ProjectionBindingExpression error) and #29014 (expanding navigations written on the grouping key) look adjacent but fail at different stages; navigation members inside the group key actually translate fine in the shape below.

Your code

### Minimal repro (self-contained)


using Microsoft.EntityFrameworkCore;

using var ctx = new ReproContext();
Console.WriteLine(ctx.Entries
    .GroupBy(e => e.RowNumber)
    .Select(g => g.OrderBy(x => x.Id).First())
    .OrderBy(e => e.ImportHistory.FileName)
    .ToQueryString()); // throws InvalidOperationException

public class ImportHistory
{
    public int Id { get; set; }
    public string FileName { get; set; }
}

public class ImportEntry
{
    public int Id { get; set; }
    public int RowNumber { get; set; }
    public ImportHistory ImportHistory { get; set; }
}

public class ReproContext : DbContext
{
    public DbSet<ImportEntry> Entries => Set<ImportEntry>();

    protected override void OnConfiguring(DbContextOptionsBuilder options)
        => options.UseSqlServer("Server=.;Database=repro;Integrated Security=True;TrustServerCertificate=True");
}


**Workaround** we're using in production: select one representative id per group first, then query full entities by those ids — navigations survive:


var ids = context.Entries.GroupBy(e => e.RowNumber).Select(g => g.Min(e => e.Id));
var query = context.Entries.Where(e => ids.Contains(e.Id)).OrderBy(e => e.ImportHistory.FileName);

Stack traces

System.InvalidOperationException: The LINQ expression 'DbSet<ImportEntry>()
    .GroupBy(i => i.RowNumber)
    .Select(g => g
        .AsQueryable()
        .OrderBy(e => e.Id)
        .First())
    .OrderBy(e0 => e0.ImportHistory.FileName)' could not be translated. Additional information:
Translation of member 'ImportHistory' on entity type 'ImportEntry' failed. This commonly
occurs when the specified member is unmapped.

Verbose output


EF Core version

10.0.5

Database provider

Microsoft.EntityFrameworkCore.SqlServer

Target framework

.NET 10

Operating system

Windows 11

IDE

Visual Studio 2026

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions