From 7d521691becd8d268f5be7b51d7e9f97e5bfbf96 Mon Sep 17 00:00:00 2001 From: Shay Rojansky Date: Thu, 5 Feb 2026 20:48:06 +0100 Subject: [PATCH] Document MaxBy/MinBy See https://github.com/dotnet/efcore/issues/25566 --- .../core/what-is-new/ef-core-11.0/whatsnew.md | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/entity-framework/core/what-is-new/ef-core-11.0/whatsnew.md b/entity-framework/core/what-is-new/ef-core-11.0/whatsnew.md index d3c56b5829..ffb6bf8c49 100644 --- a/entity-framework/core/what-is-new/ef-core-11.0/whatsnew.md +++ b/entity-framework/core/what-is-new/ef-core-11.0/whatsnew.md @@ -77,6 +77,33 @@ This enhancement removes a significant limitation when modeling complex domain h For more information on inheritance mapping strategies, see [Inheritance](xref:core/modeling/inheritance). +## LINQ and SQL translation + + + +### MaxBy and MinBy + +EF Core now supports translating the LINQ `MaxByAsync` and `MinByAsync` methods (and their sync counterparts). These methods allow you to find the element with the maximum or minimum value for a given key selector, rather than just the maximum or minimum value itself. + +For example, to find the blog with the most posts: + +```csharp +var blogWithMostPosts = await context.Blogs.MaxByAsync(b => b.Posts.Count()); +``` + +This translates to the following SQL: + +```sql +SELECT TOP(1) [b].[Id], [b].[Name] +FROM [Blogs] AS [b] +ORDER BY ( + SELECT COUNT(*) + FROM [Post] AS [p] + WHERE [b].[Id] = [p].[BlogId]) DESC +``` + +Similarly, `MinByAsync` orders ascending and returns the element with the minimum value for the key selector. + ## Cosmos DB