Find Duplicates in a datatable using Filter Datatable activity in Uipath

Hi,

Can anyone please suggest how to compare the duplicate rows in Datatable(inputDT)and Store the Duplicates in another Datatable(outputDT) by looping through each row and comparing each row using Filter Datatable activity.

Sample Input File:
DT: inputDT
Test1

Sample Output File:

Thanks In advance.

Hi @Srividya_N ,

Use following syntax in assign activity and assign this to datatable:
In Assign stage:

outputDT = inputDT.AsEnumerable.GroupBy(function(r) r("Id")).Where(function(r) r.Count() > 1).SelectMany(function(l) l).ToArray().CopyToDatatable

Also make a note that duplicate in above syntax is checked on Id column and you can replace as per your requirements.

2 Likes

Hi I have done some research and I found a way of getting those datatables using LINQ. I hope it help someone

Get data table with unique rows:
dt_uniques=dt_input.AsEnumerable().Distinct(comparer:=DataRowComparer.Default).CopyToDataTable()

Get data table of duplicate rows:

1.) Get row index list of unique rows:

list_rowIndexUniques = dt_input.AsEnumerable().Distinct(comparer:= DataRowComparer.Default).Select(Function(row) dt_input.Rows.IndexOf(row)).ToList()

2.) Get data table with the rows that are not within the list of indexes

dt_duplicates=dt_input.AsEnumerable().Where(Function(row) Not list_rowIndexUniques .Contains(dt_input.Rows.IndexOf(row)) ).CopyToDataTable()

1 Like

Thanks for the solution and if there are so many rows with duplicates and need to group them and store in one datatable and perform sum up the Total column.

@Srividya_N

Once you get datatable of duplicate records as per above code, you can use below syntax to sum total for column.

Note that in below syntax is for “Total” column.

outputDT.AsEnumerable.Sum(Function(r) Convert.ToDouble(r("Total").ToString.Trim) ).ToString

I hope it will help you.