Linq 求和汇总
健者天行 2016-08-10 09:42:10 下面是我分组时候的求和, 现在我想不要分组,仅求于 Qty1 ~ Qty 15,
请教 Linq 如何写???
var q = from p in dtQtyDetail.AsEnumerable() //Linq Group By 求和汇总
group p by new
{
ColorID = p.Field<string>("ColorID"),
VampMaterial = p.Field<string>("VampMaterial"),
} into g
select new
{
ColorID = g.Key.ColorID,
VampMaterial = g.Key.VampMaterial,
Qty1 = g.Sum(p => p.Field<Int16>("Qty1")),
Qty2 = g.Sum(p => p.Field<Int16>("Qty2")),
Qty3 = g.Sum(p => p.Field<Int16>("Qty3")),
Qty4 = g.Sum(p => p.Field<Int16>("Qty4")),
Qty5 = g.Sum(p => p.Field<Int16>("Qty5")),
Qty6 = g.Sum(p => p.Field<Int16>("Qty6")),
Qty7 = g.Sum(p => p.Field<Int16>("Qty7")),
Qty8 = g.Sum(p => p.Field<Int16>("Qty8")),
Qty9 = g.Sum(p => p.Field<Int16>("Qty9")),
Qty10 = g.Sum(p => p.Field<Int16>("Qty10")),
Qty11 = g.Sum(p => p.Field<Int16>("Qty11")),
Qty12 = g.Sum(p => p.Field<Int16>("Qty12")),
Qty13 = g.Sum(p => p.Field<Int16>("Qty13")),
Qty14 = g.Sum(p => p.Field<Int16>("Qty14")),
Qty15 = g.Sum(p => p.Field<Int16>("Qty15")),
TotalQty = g.Sum(p => p.Field<Int32>("TotalQty"))
};
相当于 SQL:
select
SUM(Qty1)[Qty1],SUM(Qty2)[Qty2],SUM(Qty3)[Qty3],........
from 表
不分组 求和多列