如何更快地从 Linq 中的行中获取计数

How to get count from rows in Linq faster

我有一个 linq 可以按 Id 获取所有行计数组。 因为我的 table 非常大,所以在 Linq 中需要很长时间(因为从 EF Core 3.0 开始我必须完整地获取数据)。

            var reportCountData = this.context.ReportData.AsEnumerable().GroupBy(x => x.ReportId).ToDictionary(x => x.Key, x => x.Count());

如何让它更快(例如,不先获取所有数据)?

应翻译以下内容:

var reportCountData = from p in this.context.ReportData
            group p by p.ReportId into g
            select new
            {
                g.Key,
                Count = g.Count()
            };

https://docs.microsoft.com/de-de/ef/core/querying/complex-query-operators#groupby