List result = new List(); mesStocks.GroupBy(x => new { x.Deid, x.Pno, x.Sno }, (x, y) => { var total = y.Sum(a => a.TotalNums); var res = y.Select(stock => { stock.TotalNums = total; return stock; }).ToList(); result.Add(res.First()); return res; }).ToList();
测试代码:
static void Main(string[] args) { var test = new List { new MesStock { Deid="1",Pno="1",Sno="1",TotalNums=1}, new MesStock { Deid="1",Pno="1",Sno="1",TotalNums=1}, new MesStock { Deid="1",Pno="1",Sno="1",TotalNums=11}, new MesStock {Deid="2",Pno="2",Sno="2",TotalNums=2}, new MesStock {Deid="2",Pno="2",Sno="2",TotalNums=2}, new MesStock {Deid="2",Pno="2",Sno="2",TotalNums=33}, new MesStock {Deid="3",Pno="3",Sno="3",TotalNums=3}, }; List res = new List(); test.GroupBy(x => x.Deid + x.Pno + x.Sno, (x, y) => { var total = y.Sum(a => a.TotalNums); var tt = y.Select(t => { t.TotalNums = total; return t; }).ToList(); res.Add(tt.First()); return tt; }).ToList(); foreach (var item in res) { Console.WriteLine(item.TotalNums); } }
public static class GroupHelper { public static List> Group(this List
暂时没有评论,来抢沙发吧~