问题
I currently have the following code:
var result = query<Items>()
.Where(x => x.Id == someId)
.SelectMany(x => x.SubItems)
.GroupBy(x => x.SubItemId)
.Select(x => new ItemModel
{
SubItemId = x.Key,
SpecialItemCount = x.Where(y => y.IsSpecial == false).Count(),
})
.ToList()...
When I call "Count()" it queries all subitem count ignoring my predicate "y.IsSpecial". I tried that on EF Core 2.0.X and EF Core 2.1 preview 2, is that possibly a bug?
回答1:
You need to upgrade to EF Core 2.1 where GroupBy is supported : https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-2.1
回答2:
Same problem here with EF Core 3.0. Seems to be bug in EF - you can workaround by using
SpecialItemCount = x.Sum(y => y.IsSpecial ? 0 : 1)
instead of
SpecialItemCount = x.Count(y => y.IsSpecial == false)
来源:https://stackoverflow.com/questions/50160399/ef-core-2-groupby-count-predicate-ignored