Skip to content

Analyzer warning and quick fix for ToDictionary over GroupBy #16838

@roji

Description

@roji

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions