linq 分组求和
var qs = SNP.GroupBy(x => new { x.customer_name, x.sales, x.customer_code}) ;
我要分组并求trans_fee的和,我怎么再把sum(trans_fee)加上去,高人指点
[最优解释]
var qs = SNP.GroupBy(x => new { x.customer_name, x.sales, x.customer_code}).Select(g=>new{trans_feeSum=g.Sum(x=>x.trans_fee),g.customer_name,g.sales,g.customer_code});
[其他解释]
var qs = SNP.GroupBy(x => new { x.customer_name, x.sales, x.customer_code})
.Select(g=>new{trans_feeSum=g.Sum(x=>x.trans_fee)} ;
[其他解释]
var qs = SNP.GroupBy(x => new { x.customer_name, x.sales, x.customer_code}).Select(g=>new{trans_feeSum=g.Sum(x=>x.trans_fee)});
foreach (var qs_ in qs)
{
Response.Write(qs_.trans_feeSum+"--"+qs_.);//这个只有trans_feeSum,没有customer_name这些
}
[其他解释]
var qs = SNP.GroupBy(x => new { x.customer_name, x.sales, x.customer_code})
.Select(g=>new{
customer_name=g.Key.customer_name,
customer_code=g.Key.customer_code,
sales=g.Key.sales,
trans_feeSum=g.Sum(x=>x.trans_fee)
} ;