Faster Queries in EF Core 11: Joins, GroupBy and MaxBy
Special Thanks to Our Sponsors:
EF Core is too slow? Discover how you can easily insert 14x faster (reducing saving time by 94%).
Boost your performance with our method within EF Core: Bulk Insert, update, delete and merge.
Thousands of satisfied customers have trusted our library since 2014.
馃憠 Learn more
When an application grows, so do its queries. Includes, joins and groupings pile up and the generated SQL slowly becomes the bottleneck.
Most of the time we only notice it when the database starts to struggle. Rewriting LINQ by hand just to get a leaner query is tedious and easy to get wrong.
To address this, EF Core 11 improves how several common query shapes are translated. Better joins, smarter grouping and new operators mean less work for the database without touching your code.
In this blog, I'll walk through the query improvements I find the most useful.
Better SQL for To-One Joins
Let's start with a query most of us write all the time. We load orders with their customer and items using a split query:
var orders = await dbContext.Orders
.Include(o => o.Customer)
.Include(o => o.Items)
.AsSplitQuery()
.ToListAsync();
Before EF Core 11, the split query for items also joined the customer, even though no customer columns were needed. That join was pure overhead.
I ran this against SQL Server 2025. The query for the items only touches the tables it actually reads, there is no customer join:
SELECT [o0].[Id], [o0].[OrderId], [o0].[ProductId], [o0].[Quantity], [o].[Id]
FROM [Orders] AS [o]
INNER JOIN [OrderItems] AS [o0] ON [o].[Id] = [o0].[OrderId]
ORDER BY [o].[Id]
The customer is loaded by its own query, which joins Orders and Customers and orders by the order key. The docs also say EF no longer adds redundant keys of reference navigations to the ORDER BY clause, the parent key already identifies the row.
According to the official docs, one split query scenario improved by 29% and one single query scenario by 22%. I did not benchmark this, and your numbers will depend on your schema and data, but you get it for free after upgrading.
If you want to refresh how split queries work, check out my previous blog post: Split Queries with EF Core in .NET.
GroupBy Enhancements
EF Core 11 also improves how GroupBy queries are translated. Aggregates over reference navigations are translated with a join before the grouping.
Here is a simple example that sums the price from a reference navigation per order:
var totals = await dbContext.OrderItems
.GroupBy(i => i.OrderId)
.Select(g => new OrderTotalResponse(
g.Key,
g.Sum(i => i.Product.Price * i.Quantity)))
.ToListAsync();
The LINQ is written exactly as before. This is the SQL I got, a single INNER JOIN and a GROUP BY, no correlated subquery:
SELECT [o].[OrderId], COALESCE(SUM([p].[Price] * CAST([o].[Quantity] AS decimal(18,2))), 0.0)
FROM [OrderItems] AS [o]
INNER JOIN [Products] AS [p] ON [o].[ProductId] = [p].[Id]
GROUP BY [o].[OrderId]
You can also access a navigation on the top item of each group. This returns the product name of the largest line per order:
var lines = await dbContext.OrderItems
.GroupBy(i => i.OrderId)
.Select(g => new LargestLineResponse(
g.Key,
g.MaxBy(i => i.Quantity)!.Product.Name))
.ToListAsync();
It translates, but this one does not use a join over the group. EF generates a correlated subquery with TOP(1):
SELECT [o].[OrderId], (
SELECT TOP(1) [p].[Name]
FROM [OrderItems] AS [o0]
INNER JOIN [Products] AS [p] ON [o0].[ProductId] = [p].[Id]
WHERE [o].[OrderId] = [o0].[OrderId]
ORDER BY [o0].[Quantity] DESC)
FROM [OrderItems] AS [o]
GROUP BY [o].[OrderId]
FullJoin
.NET 11 adds a first-class FullJoin LINQ operator, and EF Core 11 translates it to a FULL JOIN. It keeps rows from both sides and matches them when the keys are equal.
That is handy for finding customers without orders in a single query. Rows without a match on the other side come back as null, so the result selector uses the null-forgiving operator:
var results = await dbContext.Customers
.FullJoin(
dbContext.Orders,
c => c.Id,
o => o.CustomerId,
(c, o) => new FullJoinResponse(c!.Name, o!.Id))
.ToListAsync();
The generated SQL is exactly what you would expect:
SELECT [c].[Name], [o].[Id]
FROM [Customers] AS [c]
FULL JOIN [Orders] AS [o] ON [c].[Id] = [o].[CustomerId]
In my sample every order requires a customer, so the null rows only appear on the order side for customers who never ordered. If your model allows orders without a customer, those rows would show up as well.
I've covered the other join types in a dedicated blog post that you can check out here if you're interested: LeftJoin and RightJoin Operators in EF Core.
MaxBy and MinBy
MaxBy and MinBy return the whole element with the highest or lowest key, not just the value. EF Core 11 translates MaxByAsync and MinByAsync, and their sync counterparts, to SQL.
Before, we had to order and take the first row ourselves:
// Before EF Core 11
var product = await dbContext.Products
.OrderByDescending(p => p.Price)
.FirstOrDefaultAsync();
// EF Core 11
var product = await dbContext.Products
.MaxByAsync(p => p.Price);
Both versions return the same product, and both run as SELECT TOP(1) with ORDER BY Price DESC.
The key selector can also be an aggregate over a navigation. EF generates SELECT TOP(1) with an ORDER BY over the subquery:
var bestSeller = await dbContext.Products
.MaxByAsync(p => p.OrderItems.Sum(i => i.Quantity));
MinByAsync works the same way, only the ordering is ascending.
Top One Per Group
A very common requirement is getting one row per group, like the cheapest product in each category. Combining GroupBy with MinBy expresses it directly:
var cheapestPerCategory = await dbContext.Products
.GroupBy(p => p.Category)
.Select(g => g.MinBy(p => p.Price)!)
.ToListAsync();
On SQL Server this is a single query. EF numbers the rows per category with ROW_NUMBER, keeps the first one and joins the result back to the distinct categories:
SELECT [p3].[Id], [p3].[Category], [p3].[Name], [p3].[Price]
FROM (
SELECT [p].[Category]
FROM [Products] AS [p]
GROUP BY [p].[Category]
) AS [p1]
LEFT JOIN (
SELECT [p2].[Id], [p2].[Category], [p2].[Name], [p2].[Price]
FROM (
SELECT [p0].[Id], [p0].[Category], [p0].[Name], [p0].[Price],
ROW_NUMBER() OVER(PARTITION BY [p0].[Category] ORDER BY [p0].[Price]) AS [row]
FROM [Products] AS [p0]
) AS [p2]
WHERE [p2].[row] <= 1
) AS [p3] ON [p1].[Category] = [p3].[Category]
There is no client evaluation and no N+1 queries. The exact shape can change between versions, so treat this as what EF Core 11 generated for me.
Top N Per Group
What if you need the two cheapest products per category instead of one? The same ROW_NUMBER translation works, as long as you shape the query the right way.
The obvious version with SelectMany does not translate. EF throws an InvalidOperationException saying the LINQ expression could not be translated:
// Throws: could not be translated
var products = await dbContext.Products
.GroupBy(p => p.Category)
.SelectMany(g => g.OrderBy(p => p.Price).Take(2))
.ToListAsync();
Selecting the ordered and limited group into a result translates fine:
var categories = await dbContext.Products
.GroupBy(p => p.Category)
.Select(g => new CategoryProductsResponse(
g.Key,
g.OrderBy(p => p.Price).Take(2).Select(p => p.Name).ToList()))
.ToListAsync();
It is the same ROW_NUMBER shape as before, only the filter changes to two rows per category, and the result is one row per category with its two product names:
SELECT [p1].[Category], [p3].[Name], [p3].[Id]
FROM (
SELECT [p].[Category]
FROM [Products] AS [p]
GROUP BY [p].[Category]
) AS [p1]
LEFT JOIN (
SELECT [p2].[Name], [p2].[Id], [p2].[Category], [p2].[Price]
FROM (
SELECT [p0].[Name], [p0].[Id], [p0].[Category],
ROW_NUMBER() OVER(PARTITION BY [p0].[Category] ORDER BY [p0].[Price]) AS [row], [p0].[Price]
FROM [Products] AS [p0]
) AS [p2]
WHERE [p2].[row] <= 2
) AS [p3] ON [p1].[Category] = [p3].[Category]
ORDER BY [p1].[Category], [p3].[Category], [p3].[Price]
I've also written an example project with all of these queries and the SQL they generate. You can check it out in the source code link at the end of the blog.
Conclusion
EF Core 11 makes everyday queries cheaper and more expressive. Pruned joins and leaner ordering speed up your Include queries just by upgrading.
Aggregates over navigations become joins, FullJoin covers a join type we had to emulate, and MaxBy and MinBy remove the order-and-take boilerplate. Top one and top N per group translate to a single ROW_NUMBER query, as long as you project the group instead of using SelectMany.
Keep in mind that EF Core 11 requires the .NET 11 SDK and runtime.
For the full list of changes, see the official docs: What's New in EF Core 11.
If you want to check out examples I created, you can find the source code here:
Source CodeI hope you enjoyed it, subscribe and get a notification when a new blog is up!
Keep reading
Related posts

LeftJoin and RightJoin Operators in EF Core
EF Core 10's introduction of native LeftJoin and RightJoin operators marks a significant simplify how we write join statements.

Getting Started with C# Eval Expression
The C# Eval Expression Library extends LINQ with dynamic capabilities, allowing you to build runtime queries while maintaining familiar syntax and performance benefits.

Improvements in .NET 9
.NET 9 brings performance boosts, LINQ optimizations, exception handling improvements, Time-Ordered GUIDs, feature switches, and more for better efficiency.
