Linq query for excel automation

I have multiple columns like,
Ex: Col1 Col2 Sum
2 3
4 5
6 7

Now by adding col1 and col2, I want to get sum column as,
Sum
5
9
13
Using linq query???..

UpdateDataRowsLinq.xaml (7.9 KB)

@Manish540

project.json (894 Bytes)

Thanks!

1 Like

File is not opening…
Can you send me the whole folder with .json file…

Updated @Manish540 - Please check.

Dt.AsEnumerable.ToList.ForEach(Sub(row) row(“Sum”)= (val(row(“Col1”).tostring)+val(row(“Col2”).tostring)))
Now I want to copy to datatable variable, how to do that???..
In assign activity, how to do that???..

Dt.AsEnumerable.ToList.ForEach(Sub(row) row(“Sum”)= (val(row(“Col1”).tostring)+val(row(“Col2”).tostring))) - this will return datatable only.

If you want sum in another Dt variable, take one copy first before updating.

UpdateDataRowsLinq.xaml (8.8 KB)

Please have a look. I’m storing the sum dt in another variable.

Thanks!

I want to do it in one assign activity, how to do it???..

@Manish540,

You can do that in Excel with simple Excel formula, why you need to do that from UiPath.

I need to do it in assign activity only…
So Please help?..

@Manish540
Do you need in assing, for each row you want the sum in a variable in Assign activity.

Please confirm the below,

  1. Do you want to write the sum value to the excel?
    or want it in a variable in Uipath Process?

After doing sum, I want it in Datatable Variable…

1 Like

Hello @Manish540
You can do this using single query of linq use this code in an Assign Activity

the flow is simple i have a datatable of two columns i read the Datatable and add a column using Add Column Activity and then use this query to get the desired output.

(From roww In DT.AsEnumerable() group By x=New with {key .id=roww("A"),key .idd=roww("B")} into gg=group select DT.Clone.LoadDataRow(new object() {gg(0)(0),gg(0)(1),gg.Sum(function(n) CInt(n("A"))+CInt(n("B")) ) },false )).CopyToDataTable

Check the workflow for better understanding

GroupBy_Sum.zip (13.9 KB)

Understand the code properly and then implement it by yourself

2 Likes

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