How to select columns and sum of columns using gro

2019-08-24 03:41发布

问题:

i am having a DataTable called dtTest. i just need to select columns from the C# Datatable using Linq concepts

Data Table Name is DtTest.

Example : DtTest Contains the following data's,

i want the result as below, that is i need the SUM of Qty and Amount columns based on group INO...

Can anyone Please help me to solve this....

回答1:

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"))
   }
);