假设我有下表:
user type amount
2 a 10
2 b 20
2 c 40
3 a 15
3 b 20
3 c 45
我想将 (c) 数量替换为 (c - (a+b)) 按用户和类型分组,我该怎么做?谢谢
最佳答案
这里有一个方法:
更新(现在使用Sum
):
from item in table
group item by item.user into g
let a = (from i in g where i.type == "a" select i.amount).Sum()
let b = (from i in g where i.type == "b" select i.amount).Sum()
let c = (from i in g where i.type == "c" select i.amount).Sum()
select c - (a + b);
关于c# - LINQ 中的分组、求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8972935/