c#linq去除重复项并将相同数据的数量字段值相加

这是执行前和执行后想要的效果

 

以下是用Sql语句实现的代码:

select goodsno, goodsspec,SUM([count]) as count  from goods 
group  by  goodsno ,goodsspec

 

下面这个则是用Linq实现的代码:

var res = (from l in picModel
                           group l by new { goodsno = l.GoodsNO, goodsSpec = l.SpecName, RecID = l.RecID, Position = l.Position } into g
                           select new { goodsno = g.Key.goodsno, goodsSpec = g.Key.goodsSpec, count = g.Sum(c => c.Count), RecID = g.Key.RecID, Position = g.Key.Position }).ToList();

 

posted @ 2017-06-28 10:29  沉迷编程的程序员  阅读(2552)  评论(0编辑  收藏  举报