Srividya_N
(Srividya N)
November 6, 2021, 8:15am
1
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
Sample Output File:
Thanks In advance.
Rahul_S
(Robot_L)
November 6, 2021, 10:53am
2
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
asgRPA
November 6, 2021, 11:14am
3
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
Srividya_N
(Srividya N)
November 6, 2021, 1:15pm
4
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.
Rahul_S
(Robot_L)
November 6, 2021, 1:46pm
5
@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.