Closed AndersMalmgren closed 3 years ago
GroupBy support is very limited in EF Core and have found myself to try different workarounds whenever I would have liked a simple GroupBy.
Perhaps it would be possible to get around it by using something like (untested)
HasMultipleSystems = info.ActiveAndOpenAgreements.Select(agp => agp.Agreement.ExternalSystemId).Distinct().Count() > 1
or resolve again from db root (pseudocode since I do not know your table layout)
HasMultipleSystems = db.Set<ExternalSystem>().Where(s => info.ActiveAndOpenAgreements.Contains(agp => agp.Agreement.ExternalSystemId == s.Id)).Count() > 1
Thats sad to hear, this used to work just fime in Ef 6. Hope they are working on getting it supported.
Edit: will try the distinct approuch
@joakimriedel Distinct actually did work, which is fine in this case. But maybe you want to operate on that data in a grouped by fashion than distinct will not cut it. Thanks for intput!
edit: I played aroudn some more, on another data entity this time, same idea though, this works
db.Set<DataAccess.Entities.dbo.Party>()
.Select(p => new
{
p.PartyName,
p.PartyNumber,
HasDuplicates = p
.CreditAccounts
.GroupBy(ca => ca.AccountNumber).Any(grp => grp.Count() > 1)
})
.ToListAsync();
This does not
db.Set<DataAccess.Entities.dbo.Party>()
.Select(p => new
{
p.PartyName,
p.PartyNumber,
Duplicates = p
.CreditAccounts
.GroupBy(ca => ca.AccountNumber).Where(grp => grp.Count() > 1)
})
.ToListAsync();
Gives Client side GroupBy is not supported.
Sensitive bugger :D
@AndersMalmgren happy to hear that, let's hope for a more capable query processor in the near future
@joakimriedel Yeah agreed. I updated above with some more interesting findings :D
This works in 6.0 release.
@AndersMalmgren The 2nd query posted in https://github.com/dotnet/efcore/issues/21036#issuecomment-636907978 does not work because projection contains groupings. Where preserves the enumerable of groupings which cannot translated to server. First query works since it is applying Any operation over grouping so at that point, grouping can be represented in server as enumerable of grouping key.
I use latest EF core, time of writing, 3.1.4
This is the query
I have tried doing this both in one select and with two selects as above, same outcome. The problem is that I do a GroupBy to find out if we got duplicate rows. When executing this query I get
Projected SQL looks like