Skip to content

Commit 29f3f69

Browse files
committed
Document MaxBy/MinBy (#5259)
See dotnet/efcore#25566
1 parent a7351ce commit 29f3f69

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

  • entity-framework/core/what-is-new/ef-core-11.0

entity-framework/core/what-is-new/ef-core-11.0/whatsnew.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,33 @@ This enhancement removes a significant limitation when modeling complex domain h
7777

7878
For more information on inheritance mapping strategies, see [Inheritance](xref:core/modeling/inheritance).
7979

80+
## LINQ and SQL translation
81+
82+
<a name="linq-maxby-minby"></a>
83+
84+
### MaxBy and MinBy
85+
86+
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.
87+
88+
For example, to find the blog with the most posts:
89+
90+
```csharp
91+
var blogWithMostPosts = await context.Blogs.MaxByAsync(b => b.Posts.Count());
92+
```
93+
94+
This translates to the following SQL:
95+
96+
```sql
97+
SELECT TOP(1) [b].[Id], [b].[Name]
98+
FROM [Blogs] AS [b]
99+
ORDER BY (
100+
SELECT COUNT(*)
101+
FROM [Posts] AS [p]
102+
WHERE [b].[Id] = [p].[BlogId]) DESC
103+
```
104+
105+
Similarly, `MinByAsync` orders ascending and returns the element with the minimum value for the key selector.
106+
80107
## Cosmos DB
81108

82109
<a name="cosmos-transactional-batches"></a>

0 commit comments

Comments
 (0)