【问题标题】:How to get count from rows in Linq faster如何更快地从 Linq 中的行中获取计数
【发布时间】:2019-10-31 18:32:49
【问题描述】:

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

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

如何加快速度(例如,不先获取所有数据)?

【问题讨论】:

    标签: entity-framework ef-core-3.0


    【解决方案1】:

    以下内容应翻译:

    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

    【讨论】:

    • 为什么是这个而不是我的?
    • .GroupBy(..) 返回 IGrouping 类型的结果。 EF 无法为此翻译数据库结构。在我的建议中,翻译是稍后通过选择具有两个标量值的新结果来完成的
    • “为什么是这个而不是我的?” AsEnumerable() 运行查询,获取所有结果到客户端,GroupBy 发生在那里。
    • 哦,我不知道 group by 基本上是在这里列出...谢谢!
    猜你喜欢
    • 2018-04-16
    • 1970-01-01
    • 2018-10-21
    • 2021-04-28
    • 1970-01-01
    • 2017-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多