You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Translate GroupBy followed by FirstOrDefault (and similar) using ROW_NUMBER() #36380
changed the title [-]How to extract the first data after grouping in EF core?[/-][+]Translate GroupBy followed by FirstOrDefault (and similar) using window function[/+]on Jul 17, 2025
@chenghehe the LINQ query as your wrote it is currently not translatable to SQL, since SQL GROUP BY requires an aggregate function (MAX, SUM...) to reduce the group back to a table; see this doc page for more information on this. Such queries typically require the use of SQL window functions, but these aren't yet supported by EF (see #12747).
In the meantime, if you're OK with bringing the rows back from the database and performing the OrderBy and FirstOrDefault on the client, add AsEnumerable() for client evaluation after the GroupBy (but for large numbers of rows this could be problematic for performance).
Am putting this issue in the backlog to track translating the above query pattern via window functions:
changed the title [-]Translate GroupBy followed by FirstOrDefault (and similar) using window function[/-][+]Translate GroupBy followed by FirstOrDefault (and similar) using ROW_NUMBER()[/+]on Aug 6, 2025
Activity
[-]EF core 中如何分组后取出第一条数据?[/-][+]How to extract the first data after grouping in EF core?[/+]chenghehe commentedon Jul 17, 2025
abnormal information:The given key 'EmptyProjectionMember' was not present in the dictionary
[-]How to extract the first data after grouping in EF core?[/-][+]Translate GroupBy followed by FirstOrDefault (and similar) using window function[/+]roji commentedon Jul 17, 2025
@chenghehe the LINQ query as your wrote it is currently not translatable to SQL, since SQL GROUP BY requires an aggregate function (MAX, SUM...) to reduce the group back to a table; see this doc page for more information on this. Such queries typically require the use of SQL window functions, but these aren't yet supported by EF (see #12747).
In the meantime, if you're OK with bringing the rows back from the database and performing the OrderBy and FirstOrDefault on the client, add AsEnumerable() for client evaluation after the GroupBy (but for large numbers of rows this could be problematic for performance).
Am putting this issue in the backlog to track translating the above query pattern via window functions:
The same can be done for ElementAt, Last, etc.
[-]Translate GroupBy followed by FirstOrDefault (and similar) using window function[/-][+]Translate GroupBy followed by FirstOrDefault (and similar) using ROW_NUMBER()[/+]m-gasser commentedon Aug 6, 2025
bachratyg commentedon Aug 6, 2025
roji commentedon Aug 6, 2025
m-gasser commentedon Aug 7, 2025
roji commentedon Aug 7, 2025
QHarshil commentedon Oct 5, 2026
I've opened #39173 for this. It translates the query to the
ROW_NUMBER()filter alone, without theGROUP BYand the join back.Query: translate GroupBy element reads with ROW_NUMBER only
Query: translate GroupBy element reads with ROW_NUMBER only (dotnet#3…