在linq到sql的sum和group by

时间:2014-08-02 06:04:59

标签: c# linq

我有一个linq,它返回如下所示的值

enter image description here

我尝试了下面的代码来总结团队的关联点,

我希望获得关联积分和团队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

2 个答案:

答案 0 :(得分:2)

由于您按iTeamId进行分组,因此您只需从群组iTeamId获取每个群组Key

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
};

答案 1 :(得分:1)

var result   = orderForBooks
.GroupBy(t => t.iTeamId )
.Select(tm => new ResultObj
        {
            teamid= tm.Key,
            points = tm.Sum(c => c.Associate_Points)
        }).ToList();