DataTable select distinct and sum

本文关键字:sum and distinct select DataTable | 更新日期: 2024-09-08 06:52:41

我有一个数据集dsItemDtls,它包含列Size CodeQuantity

尺寸代码是41,42,41类似,数量是1,2,2,我得到的输出像这个

41 |42 |41 |42 |41 |
____________________
1  | 2 |1  |2  |2  |
____________________

但我需要这样的输出:

41 |42
_______
4  |4

我试过这个代码:

DataRow[] result = ddd.Table.Select("group by SizeCode,Quantity");

DataTable select distinct and sum

我认为下面的查询将帮助您

var result = from r in dt.AsEnumerable()
             group r by new { SizeCode = r["SizeCode"] } into g
             select new { SizeCode  = g.Key.SizeCode ,                           
             Quantity = g.Sum(x => Convert.ToInt32(x["Quantity"])) };