LinQ query for grouping the specific column and sum the column


Here is my input table and I want all the col1 to be
Merged and col2 should be summed with respect to col1

Col1 Col2 Col3 Col4
xxx 10.5 vijay hai
xxx 11.5 harish krish
yyy 15.5 mani good
yyy 20.5 abhi great
yyy 10.5 vishnu yellow
yyy 30.5 ram red
zzz 25.5 prash white
zzz 5.5 hello blue

Output should be like below,

Col1 Col2
xxx 22
yyy 77
zzz 31

Can someone help with the linq query to perform this?

Hi @Harsha_Vemula

Please try this query:

outputTable = (From row In inputTable.AsEnumerable()
               Group row By col1 = row.Field(Of String)("col1") Into Group
               Select outputRow = inputTable.NewRow() 

For Each row In outputTable.Rows.Cast(Of DataRow)()
    row("col2") = inputTable.AsEnumerable().
                  Where(Function(x) x.Field(Of String)("col1") = row.Field(Of String)("col1")).
                  Sum(Function(x) Convert.ToDouble(x.Field(Of String)("Col2")))

Hope this helps,
Best Regards.

Build datatable - Configure 2 cols: Col1, Col2 - dtResult

Assign Activity
dtResult =

(From d in DataTableOrigVar.AsEnumerable
Group d by k=d("Col1").ToString.Trim into grp=Group
Let sm = grp.Sum(Function (g) CDbl(g("Col2").toString.Trim)).Tostring("F2")
Let ra = new Object(){k,sm}
Select r =dtResult.Rows.Add(ra)).CopyToDataTAble
1 Like

For LINQ learning also have a look here:
[HowTo] LINQ (VB.Net) Learning Catalogue - Help / Something Else - UiPath Community Forum

And for the experiments:
Understanding the 6 Debugging Panels of UiPath in the easiest way possible! - News / Tutorials - UiPath Community Forum

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.