Most efficient way to remove datatable duplicates while summing the dulpicates

Hey everyone,

Apologies for the long winded question, let me explain it better.
I want to be able to:

  1. Keep the top duplicate row
  2. Sum column of the duplicate values and add it to the top duplicate row:

Below is an example - Company column is the column i want to remove duplicates, money column is where I want to add all the companies money together. So in the bottom of the image we can see that the duplicates have been removed, the top value has been kept and the total of the duplicates money has been added.
forumquest

I have got this working using nested for each but it is slow and I would like it to be faster and not nested.

I was trying to use database.select() but didnt manage to come up with something.

Thank you for any suggestions.

There are a few approaches in this topic

1 Like

When line 10,11 is the expected out put we can do following:

Assign Activity:
dtResult = YourOriginDataTableVar.Clone

Assign Activity:
dtResult =

(From d in YourOriginDataTableVar.AsEnumerable
Group d by k=d("Company").toString.Trim into grp=Group
Let sm = grp.Sum(Function (x) CInt(x("Money").toString.Trim))
Let ra = new Object(){k, grp.First()(1), sm, grp.First()(3), grp.First()(4)}
Select r = dtResult.Rows.Add(ra)).CopyToDataTable

Also have a look here:

Hi @TheLearningBot,

Shared LINQ solution. As another solution, I am sharing a case study of Uipath events. Read Excel with readrange activity and assign inputDT.

SimpleExample.xaml (13.6 KB)

Regards,
MY