0

我有这个 SQL,我想在 Entity Framework Core 2.1 中执行:

Select ItemTypeId, Count(ItemTypeId) as Count from Items i 
where i.ParentId = 2
group by ItemTypeId 

我怎么做?

这是我想出的,但它返回零:

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();

我能找到的唯一例子是here

4

1 回答 1

1

你不需要Count = g.Count(i=> i.ItemTypeId == g.Key.ItemTypeId),而是使用g.Count().

于 2018-09-17T04:31:12.023 回答