Remove duplicated rows - in selected range

I am using the code to remove duplicated rows in my data table:

FilteredDataTable = DataTable1.AsEnumerable().Except(DataTable2.AsEnumerable(), DataRowComparer.Default).CopyToDataTable()

Please consider this:

Table a -

Comparing to table b -

I would like the bot to remove the row with the duplicates that are in column A:G

With the above code it will return no duplicates as column H and I will be different. Is there a way of configuring the code to say if in the table A:G are the same delete the entire row (including columns H and I)?

Cheers

@E.T.S

FilteredDataTable = (From row1 In DataTable1.AsEnumerable()
                    Where Not DataTable2.AsEnumerable().Any(Function(row2) row1("A").ToString() = row2("A").ToString() AndAlso
                                                                     row1("B").ToString() = row2("B").ToString() AndAlso
                                                                     row1("C").ToString() = row2("C").ToString() AndAlso
                                                                     row1("D").ToString() = row2("D").ToString() AndAlso
                                                                     row1("E").ToString() = row2("E").ToString() AndAlso
                                                                     row1("F").ToString() = row2("F").ToString() AndAlso
                                                                     row1("G").ToString() = row2("G").ToString())
                    Select row1).CopyToDataTable()


Hope this query works for you

Hey @E.T.S
I’m not sure if I understood correctly your case, but please check this solution:
BlankProcess100 (2).zip (15.9 KB)

Thank you for your reply!

Would I need to use this in an assign?

Cheers

Hi @E.T.S ,

Could you maybe check with the below Post :

For your case, we can dynamic the G column number by using the Expression below :

ColumnTake = UiPath.Excel.Helpers.ExcelUtilities.ConvertColumnLetterToColumnIndex("G")

Here, ColumnTake is an integer variable.

Then modifying the Expression in the post we get :

FilteredTable = (From d in dt1.AsEnumerable
Let raf = d.ItemArray.Take(ColumnTake).ToArray
Let chk = dt2.AsEnumerable.Any(Function (d2) d2.ItemArray.Take(ColumnTake).ToArray.SequenceEqual(raf))
Where not chk
Select r =d).CopyToDataTable

Also, to handle the Exception when no rows are filtered we can follow the below :

Note : The assumption is the Headers are not present for the data, hence the Read Range activity Add Headers property are unchecked.

@E.T.S

FilteredDataTable = (From row1 In DataTable1.AsEnumerable()
                    Where Not DataTable2.AsEnumerable().Any(Function(row2) row1("A").ToString() = row2("A").ToString() AndAlso
                                                                     row1("B").ToString() = row2("B").ToString() AndAlso
                                                                     row1("C").ToString() = row2("C").ToString() AndAlso
                                                                     row1("D").ToString() = row2("D").ToString() AndAlso
                                                                     row1("E").ToString() = row2("E").ToString() AndAlso
                                                                     row1("F").ToString() = row2("F").ToString() AndAlso
                                                                     row1("G").ToString() = row2("G").ToString())
                    Select row1).CopyToDataTable()

I hope this will work for you

Hi,

Thanks for your flow!

Please could you tell me what modification to the code do I need to do if I am looking at matches in columns A to I

Cheers

@E.T.S
By expanding the Tuple.Create method to include r(7) and r(8) , the query now groups rows based on the first nine columns (A to I), Like this:
dtCombined.AsEnumerable().GroupBy(Function(r) Tuple.Create(r(0), r(1), r(2), r(3), r(4), r(5), r(6), r(7), r(8))).Select(Function(g) g.First()).CopyToDataTable()

Hi,

Thanks for your reply! Even though I have my variables I am getting this error:

I am getting this error:

for invoke code you need to pass arguments:

@E.T.S
so, maybe try this
dtCombined.AsEnumerable().GroupBy(Function(r) New With {Key .Col0 = r(0), Key .Col1 = r(1), Key .Col2 = r(2), Key .Col3 = r(3), Key .Col4 = r(4), Key .Col5 = r(5), Key .Col6 = r(6), Key .Col7 = r(7), Key .Col8 = r(8)}).Select(Function(g) g.First()).CopyToDataTable()

Hello,

I have created my arguments but am getting this error:
image

Hello,

That doesn’t seem to remove the duplicates unfortunately

@E.T.S

Sequence6.zip (2.0 KB)
Please find the attached process hope it works for you

1 Like

@E.T.S

Please tell me if my workflow is working for you or i will try another approach

Awesome this works! Thanks so much for your help!

1 Like

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