我在上一篇文章中收到了非常有用的建议:Linq to group by 2 columns in C# - 我现在正在尝试对此进行扩展,以便我的 ViewModel 看起来像它应该用于视图的方式:
我的第一个查询:
var qbt = db.Calls.GroupBy(calls => new { calls.assignedteam, calls.status })
.Select(call => new StatusByTeamViewModel1
{
Team = call.Key.assignedteam,
Status = call.Key.status,
Number = call.Count()
}).OrderBy(z => z.Team).ThenBy(z => z.Status);
回报:
Team Number Status
ta 40 Open
ta 60 Closed
tb 58 Open
tb 40 Closed
tc 1 Open
tc 122 Closed
我想拿那张桌子,并将其转换为:
Team Open Closed
ta 40 60
tb 58 40
tc 1 122
我正在尝试下面的查询,但让自己搞混了——如果打开/关闭列,所有团队都只显示 1 或 0:
var qbt2 = qbt.GroupBy(x => x.Team)
.Select(call => new StatusByTeamAllViewModel2
{
Team = call.Key,
Open = qbt2.Where(it => it.Status == "Open" && it.Team==call.Key).Count(),
Closed = qbt2.Where(it => it.Status == "Closed" && it.Team == call.Key).Count(),
});
我也在尝试使用 LinqPad,但也很难让它在那里工作。
谢谢你的帮助,
标记