【发布时间】:2019-01-08 15:37:08
【问题描述】:
我创建了一个过滤数据集的方法。我还创建了另一种方法来计算应用过滤器的项目数量。这个方法看起来像这样:
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 项目
【问题讨论】:
-
如果有人可以帮助你,提供你创建的类的代码和你没有使用的类的正确使用是非常必要的。
-
这是 DbSet 还是 DbContext?
-
您应该将
entity称为group。它有点烦人,表明参数可能是单个实体。为什么你首先需要r => r.Address.City == entity.Key?你只需要.Count(),因为你已经对结果进行了分组,分组内容都属于group.key所属的同一个城市
标签: c# linq asp.net-core entity-framework-core