How to compare two data table

Hi Team I have a data table called DT1 and DT2
In DT1 it is stable.
In DT2 data will change every time.

With Example I explain below.

DT1:

Slno PrimaryName DivisonNo
1 ABC 1234
2 Exce 5432
3 CHET 2345

DT2:

Slno PrimaryName DivisonNo
1 MSLA 234
2 CHET 2345
3 Exce 5432

Expected OP:

1 ABC 1234
2 Exce 5432
3 CHET 2345
1 MSLA 234

Note: Every Time we are add all new Data from DT2 to DT1, These are all in the loop mode. New data were add to the DT2 so we are check all new data Dt2 with old Dt1.

If we find all three data rows are same then we need to throw exception.

Example: If the data of Dt2 all values are match with DT1 then we need to throw exception

Hi @copy_writes

You can use LinQ Expression for this.

Hope it helps!!

Can You please give the LINQ quire Please

1 Like

Hi @copy_writes

Try this

DT1.AsEnumerable().GroupBy(Function(row) row.Field(Of String)("PrimaryName")).Select(Function(group) group.OrderByDescending(Function(row) row.Field(Of Double)("DivisionNo")).First()).CopyToDataTable()

Output:

image

Cheers!!

Hi @copy_writes

Use below code in Invoke Code Activity

  1. Read both DataTables and assign into variables DT1 and DT2
  • It Check if all rows in DT2 match with DT1. IF Match Throw Eception Else Add Row to DT1.
If (Not DT2.AsEnumerable().Any(Function(row) Not DT1.AsEnumerable().Any(Function(r) r("PrimaryName").ToString().Equals(row("PrimaryName").ToString()) AndAlso r("DivisonNo").ToString().Equals(row("DivisonNo").ToString())))) Then
    Throw New Exception("Exception: All rows in DT2 match with DT1")
End If
For Each row In DT2.AsEnumerable()
    DT1.Rows.Add(row.ItemArray)
Next

Hope it’ll helps you :slight_smile:

Thanks for quick replay.

As I mentioned, If the all rows rows are matched then we need to set a flag how can we do that.

Is this work for below condition?

If out of 4 row 3 were matched and 1 is unique is this throw error?

@copy_writes

How about the following?

If:
DT1.AsEnumerable().SequenceEqual(DT2.AsEnumerable(), DataRowComparer.Default)
1 Like

No, it will not throw an error in that case. It will only throw an error if all rows in DT2 match with DT1.
If three out of four rows in DT2 match with DT1 and one row is unique, it will add that row to DT1

1 Like

When I use the above code in assign activity I am getting the bellow error.
image

Thanks, But I’m Getting Method Not found when I place your code.

I have a doubt if dt2 values are changed when it is in loop?

because every loop dt2 add with new data on that time agin we need to compare with dt1 data table and if it match throw exception if not we need to add the data row to dt1 from dt2.

DivisonNo column is not in INT or DOUBLE it is in the string

@copy_writes ,

Please check this workflow.

CheckDuplicates.xaml (17.6 KB)

Thanks,
Ashok :slight_smile:

Pleas send it in Zip File

Here you go.

CompareDataTable.zip (10.9 KB)

Thanks,
Ashok :slight_smile:

Thanks For your quick response when I try to open the project I am getting document invalid error

I am using UiPath Studio 2020.10.2 v

Can you please help me.

Best regards.
Copywrites

Hi @copy_writes

You can try simple Approach for this:

  1. Read both DataTables and assign into variables DT1 and DT2
  • Use IF Activity to check the unique rows with below expression

DT2.AsEnumerable().Except(DT1.AsEnumerable(),DataRowComparer.Default).CopyToDataTable().RowCount>0

If Rows Count is Greater than 0 - Must be Unique Rows which need to be add in DT1

  1. Use Merge Data Table Activity with
    Source
    DT2.AsEnumerable().Except(DT1.AsEnumerable(),DataRowComparer.Default).CopyToDataTable()
    Destination - DT1

ELSE - Throw Exception (For Same rows )


image

Hope it helps you to Understand :slight_smile:

1 Like

@copy_writes ,

It was throwing error due to Studio and package difference I guess.
I have downgraded the packages to lowest version available in Studio 2023.10.3

If still the whole solution don’t works, copy only the CheckDuplicate.xaml to your project and this should work as there is no dependency on other things.

CompareDataTable.zip (10.0 KB)

Thanks,
Ashok :slight_smile: