我有一个简单的Linq查询,它按一个字段Team
分组:
var qbt = db.Calls.GroupBy(x => x.team).Select(call => new
{
Team = call.Key,
Number=call.Count()
});
返回:
Team Number
ta 100
tb 98
tc 123
如何更改查询以使其具有“status”附加列,以便返回:
Team Number Status
ta 40 Open
ta 60 Closed
tb 58 Open
tb 40 Closed
tc 1 Open
tc 122 Closed
我尝试添加另一组:
var qbt = db.Calls.GroupBy(x => x.team).GroupBy(y => y.status).Select(call => new
{
Team = call.Key,
Status = call.Key2,
Number=call.Count()
});
......但那不会编译。
谢谢Mark,
答案 0 :(得分:9)
您可以对匿名类型进行分组:
.GroupBy(x => new { x.team, x.status })
使用相应的选择:
.Select(call => new
{
Team = call.Key.team,
Status = call.Key.status,
Number = call.Count()
});
答案 1 :(得分:6)
你需要在分组中创建新的匿名类型,这应该可以解决问题。
var qbt = db.Calls
.GroupBy(x => new { Team = x.team, Status = x.status })
.Select(call => new
{
Team = call.Key.Team,
Status = call.Key.Status,
Number=call.Count()
});