我正在使用以下示例数据,但我的linq返回错误结果,请帮助
id amount
1 -500
2 500
1 500
1 -500
1 -500
2 500
1 -1000
1 500
2 500
答案 0 :(得分:4)
试试这个: -
var result = list.GroupBy(x => x.id)
.Select(x => new { id = x.Key, Amount = x.Sum(z => z.amount) });
答案 1 :(得分:1)
你需要对两者进行求和和分组,所以
var output = from item in data
group item by item.Id
into g
select new { Id = g.Key, amount = g.Sum(item => item.amount) };