Ho questo SQL che vorrei eseguire in Entity Framework Core 2.1:
Select ItemTypeId, Count(ItemTypeId) as Count from Items i
where i.ParentId = 2
group by ItemTypeId
Come lo faccio?
Questo è ciò che mi è venuto in mente, ma restituisce zero:
var query = this._context.Items.Where(a => a.ParentId == 2)
.GroupBy(i => new { i.ItemTypeId })
.Select(g => new { g.Key.ItemTypeId, Count = g.Count(i=> i.ItemTypeId == g.Key.ItemTypeId) });
var items = query.ToList();
L'unico esempio che ho trovato è stato qui
Non è necessario Count = g.Count(i=> i.ItemTypeId == g.Key.ItemTypeId)
, utilizzare invece g.Count()
.