Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Analyzer warning and quick fix for ToDictionary over GroupBy #16838

Open
Tracked by #22086
roji opened this issue Jul 30, 2019 · 3 comments
Open
Tracked by #22086

Analyzer warning and quick fix for ToDictionary over GroupBy #16838

roji opened this issue Jul 30, 2019 · 3 comments

Comments

@roji
Copy link
Member

roji commented Jul 30, 2019

We've had discussions in #16730 and offline about the following:

var test = await this.dbcontext.MyEntity
        .GroupBy(x => x.Status)
        .ToDictionary(x => x.Key.Name, y => y.Count());

There is evidence to support that a common expectation is for this to get translated for server evaluation. However, since ToDictionary() is only defined over IEnumerable, EF Core cannot see the aggregate function and the grouping will in fact be performed client-side.

We can have an analyzer to detect this, and even suggest a quick fix of extracting the aggregate lambda inserting a Select() with it between the GroupBy and ToDictionary.

@divega
Copy link
Contributor

divega commented Jul 31, 2019

The analyzer should probably recognize ToLookup as well.

@ajcvickers ajcvickers added this to the Backlog milestone Aug 5, 2019
@ajcvickers ajcvickers removed this from the Backlog milestone Nov 20, 2019
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
@molinch
Copy link

molinch commented Aug 5, 2024

I would love to see such an analyzer, we just fell into this trap and it was a pain to diagnose. Especially for ToDictionaryAsync since it comes from EF.Core.

@roji
Copy link
Member Author

roji commented Aug 5, 2024

Reopening to reconsider doing this, now that we're starting to have static analysis capabilities for EF LINQ queries (via precompiled queries).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants