下面是本人分组时候的求和, 现在本人想不要分组,仅求于 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 表
不分组 求和多列
讨教 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 表
不分组 求和多列
解决方案
5
group by本来就是Linq的东西,不用这个,估计很难
或可以放到datatable处理
或可以放到datatable处理
5
select new这个匿名对象是编译产生的,假如你不计划用codedom或emit的话,只能硬写
5
分组和求和有什么关系,不分组也能求和。
150
var q = from p in dtQtyDetail.AsEnumerable() group p 1 into g select new { Qty1 = g.Sum(p => p.Field<Int16>("Qty1")), Qty2 = g.Sum(p => p.Field<Int16>("Qty2")), Qty3 = g.Sum(p => p.Field<Int16>("Qty3")) };
finally you still using a “fake” group by
35
var q = from p in dtQtyDetail.AsEnumerable() //Linq Group By 求和汇总
group p by “A” 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”))
//……
};
分组依据放一个常量,这个组就是整个表本身,这样就是整个表Qty1、Qty2、Qty3……字段的总和了
group p by “A” 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”))
//……
};
分组依据放一个常量,这个组就是整个表本身,这样就是整个表Qty1、Qty2、Qty3……字段的总和了