如何使用c#中的数据表中的group by关键字选择列和列的总和

时间:2013-08-02 11:39:37

标签: c# linq linq-to-sql

我有一个名为dtTest的DataTable。我只需要使用Linq概念从C#Datatable中选择列

数据表名称是DtTest。

示例:DtTest包含以下数据

enter image description here

我想得到如下结果,即我需要基于INO组的数量和数量的总和......

enter image description here 任何人都可以帮我解决这个问题....

1 个答案:

答案 0 :(得分:3)

DtTest
.AsEnumerable()
.GroupBy
(
   x=>
   new
   {
       BNO = x.Field<int>("BNO"),
       INO = x.Field<int>("INO"),
       Desp = x.Field<string>("Desp"),
       Rate= x.Field<decimal>("Rate")
   }
)
.Select
(
   x=>
   new 
   {
      x.Key.BNO,
      x.Key.INO,
      x.Key.Desp,
      Qty = x.Sum(z=>z.Field<int>("Qty")),
      x.Key.Rate,
      Amount = x.Sum(z=>z.Field<decimal>("Amount"))
   }
);