EF Core 2 GroupBy Count predicate ignored

情到浓时终转凉″ 提交于 2019-12-11 05:02:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!