Using Linq to GroupBy and Sum datatable

前端 未结 3 1523
感动是毒
感动是毒 2021-01-02 14:02

Hi, I have a Datatable like this:

Id             Amount 1        Amount 2        Amount 3  
1              2               2               2  
12         


        
3条回答
  •  死守一世寂寞
    2021-01-02 14:33

    You are making your calls out of order. The code below wont put the data into a table, but it will give you data you could put into a table easily.

    dt.AsEnumberable()
        .GroupBy(g => g["ID])
        .Select(g => new {
            Id = g.Key, 
            Amount1 = g.Sum(s => s.Amount1), 
            Amount2 = g.Sum(s => s.Amount2), 
            Amount3 = g.Sum(s => s.Amount3)});
    

提交回复
热议问题