C# linq到sql中的sum和group by
我有一个linq,它返回如下所示的值 我尝试了下面的代码来为团队计算关联分数 我想得到员工积分和团队id的总和C# linq到sql中的sum和group by,c#,linq,C#,Linq,我有一个linq,它返回如下所示的值 我尝试了下面的代码来为团队计算关联分数 我想得到员工积分和团队id的总和 var result = from p in orderForBooks group p by p.iTeamId into g select new { points = g.Sum(x => x.Associate_Points), teamid=g.Select(x=>x.iTeamId) }; 它对关联点求和,但不获取团队id因为您是按iTeam
var result = from p in orderForBooks
group p by p.iTeamId into g
select new
{
points = g.Sum(x => x.Associate_Points),
teamid=g.Select(x=>x.iTeamId)
};
它对关联点求和,但不获取团队id因为您是按
iTeamId
分组的,您只需从组的键
中获取每组的iTeamId
:
var result = orderForBooks
.GroupBy(t => t.iTeamId )
.Select(tm => new ResultObj
{
teamid= tm.Key,
points = tm.Sum(c => c.Associate_Points)
}).ToList();
var result = from p in orderForBooks
group p by p.iTeamId into g
select new
{
points = g.Sum(x => x.Associate_Points),
teamid = g.Key
};