neko_code
neko_code

Reputation: 586

EF Core 2 GroupBy Count predicate ignored

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?

Upvotes: 1

Views: 437

Answers (2)

Thomas Hetzer
Thomas Hetzer

Reputation: 1637

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)

Upvotes: 1

Robert Perry
Robert Perry

Reputation: 1956

You need to upgrade to EF Core 2.1 where GroupBy is supported : https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-2.1

Upvotes: 0

Related Questions