Here are 2 different to help you eliminate duplicate rows from a DataTable:
Since version 2018.4.1 UiPath contains a dedicated activity to delete duplicate rows in a DataTable. Input is a DataTable, and output is the same DataTable without the duplicate rows.
If not available, another way is transform your DataTable to a DataView and back to a DataTable.
The following is the precise code you will require to do so:
2 things you should notice: The first parameter in ToTable has to be True. This tells UiPath to return the unique rows from the table. The second thing is that if you have to list all of the column names inside a String Array: id this is not done, the return DataTable will not have the columns that were not included in the String Array.
@harizd
give a try with this xaml Main.xaml (11.0 KB)
you can make changes in xaml according to your need
content of xaml:
dt_old >> old DataTable
new datatable dt
dt.Columns.Add("Col1", GetType(String))
dt.Columns.Add("Col2", GetType(String))
dt.Columns.Add("Col3", GetType(String))
dt = (From p In dt_old.AsEnumerable()
Group By x= New With { Key.a =p.Item(“Col1”),Key.b=p.Item(“Col2”)}
Into Grp = Group Select dt.LoadDataRow (New Object() {grp(0)(0), grp(0)(1),String.Join(";",grp.select(Function(c) c("Col3")))},False)).CopyToDataTable
@harizd I am not good at explaining things but I gave a try below…
Out linq query can explained as below:
(From p In dt_old.AsEnumerable()
–here we are creating enumerable from dt_Old
Group By x= New With { Key.a =p.Item(“Col1”),Key.b=p.Item(“Col2”)} Into Grp = Group
– In this line we are grouping the datarows in groups (grp) based on Col1 and Col2 [in our case we are getting 3 groups, each containing 2,1,2 datarows respectively]
grp(0)(0)= from a group of datarow i.e grp, get first row’s first column’s value
grp.select(Function(c ) c(“Col3”))) = here we used function because in function we pass an argument and in return we get value. Here argument is datarow and value is value of Column “Col3”.
You can name your functin anything you want like abc or xyz or just a or just b etc.
Select dt.LoadDataRow (New Object() {grp(0)(0), grp(0)(1).String.Join(“;”,grp.select(Function(c ) c(“Col3”)))},False)).CopyToDataTable
– In this line we are doing three things
creating the new array of objects ie <<New Object() {grp(0)(0), grp(0)(1),String.Join(“;”,grp.select(Function(c ) c(“Col3”)))}>>
Loading that array of object in new datarow in dt with dt.LoadDataRow
selecting that datatable << dt >> as enumerable of datarow so that at the end we will get the datatable by using CopyToDataTable method