Select中的实体框架核心计数

克杰达姆

我创建了一种过滤数据集的方法。我还创建了另一种方法来计算应用过滤器的项目数量。这个方法看起来像这样:

return new Count
        {
            Address = this.Filter(/* paramters to filter */)
                .GroupBy(person => person.Address.City)
                .Select(group => new Amount<string>
                {
                    Type = group.Key /* so the city */,
                    Count = group.Count()
                }),
        };

Count 有几个数量对象列表。数量对象包含它将过滤的名称以及过滤器包含的数量的计数。

this.Filter()

是一个私有方法,它将从 DbContext 返回一个 IQueryable。

一切正常,但是,对于大量数据,它非常慢。这样做的原因是 GroupBy() 和 Count() 无法翻译,将在本地进行评估。它给出了以下警告:

warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'GroupBy([entity.RequestRating], [entity])' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'Count()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'Count()' could not be translated and will be evaluated locally.

对此有什么更好的方法?这是一个 ASP.NET CORE 项目

坦维尔·阿杰尔

试试这个:

var amountList = Persons.Where(paramters to filter)
            .GroupBy(person => person.Address.City)
            .Select(group => new Amount()
            {
                City = group.Key,
                CityCount = group.Count()
            }).Tolist();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章