Linq 求和汇总

健者天行 健者天行 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
下面是我分组时候的求和, 现在我想不要分组,仅求于 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 表


不分组 求和多列
...全文

相关推荐

评论 14
收藏 已收藏
分享
返回顶部
健者天行
C# 111,129 成员 642,538 内容