-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Translate LINQ DistinctBy #27470
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
I cannot wait for this feature to work!! 👍 |
Would be cool to translate the following too, thought let me know if you want a separate issue. _db.Posts
.GroupBy(p => p.CategoryId)
.Select(g => new
{
CategoryId = g.Key,
NumberOfPosts = g.Count(),
NumberOfTags = g.DistinctBy(p => p.TagId).Count()
}); Though perhaps semantically this is a bit weird and the current way of doing it make more sense: NumberOfTags = g.Select(p => p.TagId).Distinct().Count() |
@benmccallum yeah, just projecting first makes more sense to me: DistinctBy really is for when you want the actual entities, rather than just counting them afterwards. |
Is this feature dependent on first handling all these issues? |
@lonix1 no; the above translation with GroupBy works at least in the basic case; there may be bugs in other, non-basic cases, but those don't prevent the basic transformation. |
.NET 6.0 introduced DistinctBy, which we could translate.
DistinctBy can be rewritten as follows:
Note: since DistinctBy returns the first element with a given key, it is order-sensitive, and so we should issue a warning if it's used without OrderBy.
Note that PostgreSQL has a
DISTINCT ON
feature which is likely much more efficient than what we generate above (npgsql/efcore.pg#894).The text was updated successfully, but these errors were encountered: