Linq 求和汇总
下面是我分组时候的求和, 现在我想不要分组,仅求于 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 表
不分组 求和多列
...全文
相关推荐
DataTable分类求和,分类汇总,排序
在处理大量数据时,我们可能需要对数据进行各种操作,例如分类、求和、汇总以及排序。以下是对"DataTable分类求和,分类汇总,排序"这个主题的详细解析。1.
LinQ查询分组 ASP.NET-2008.rar
首先,让我们来深入了解一下LINQ中的分组概念。在LINQ查询中,`GroupBy()`方法用于根据一个或多个键对数据进行分组。这个操作非常有用,特别是在需要对数据进行聚合或按特定条件汇总时。
Linq Grouping GroupBy 用法详解
实际应用示例- 数据分析:在数据库查询中,`GroupBy`可用于汇总统计,例如计算每个产品的销售量。- UI呈现:在UI设计中,可能需要按照类别展示数据,如显示不同城市的天气预报。
C# LINQ聚合函数详解[源码]
其中,LINQ的聚合函数扮演着重要的角色,它们能够对数据进行汇总、计算和转换。
Linq基础学习资料,通俗易懂
- **into 汇总**:用于汇总查询的结果。#### 9. DataSource 数据绑定数据绑定是将数据源(如集合、数据库等)与用户界面元素(如列表框、表格等)连接起来的过程。


