How to Compare Two Datatables

Hello All,

I want to compare two Data tables and the data which is not common in two Datables i want in a separate Datatable so I used ‘Except’ method, which is working very fine if i try using it separately but when i am using it in my Project its giving me all the existing data instead of giving uncommon data between two datatables.Here is the below code i used

DT1.AsEnumerable.Except(Dt2.AsEnumerable,system.Data.DataRowComparer.Default).CopyToDataTable

Please let me know if theres any mistkae in the code or what could be the resson for failing in my project code but not in a seperate code

@Aswini
try this
Syntax1 : DT1.AsEnumerable.Where(Function(x) DT2.AsEnumerable.Any(Function(y) x(0).ToString.Equals(y(0).ToString) )).CopyToDataTable

syntax2: DT1.asenumerable.where(function (x,i) x(“Common Column Name”).tostring.equals(DT2(i)(“Common Column Name”).tostring) ).copytodatatable


Cheers!

here we need to specify on how common is defined

  • All Colummns to check
  • A columnset to check
  • A single column to check

Based on the definition we can go ahead for the solution approach suggestion

I possible share with us some sample data and expected output

@Aswini

if the datatype of the columns are different in two tables then it might give they are different

at that time instead of except better to use where

cheers

@Dinesh_Guptil

Thanks for responding. I want the data which is not similar. If we compare the data from the Datable which used for examples, I want Nikhil from DT1 and Sai from Dt2

Hope the below screenshot help you in understanding the scenario

@Aswini

Use the below syntax

Syntax :
DT1.AsEnumerable.Except(DT2.AsEnumerable,Datarowcomparer.Default).Concat(DT2.AsEnumerable.Except(DT1.AsEnumerable,Datarowcomparer.Default)).CopyToDataTable

Will later in Production really only 1 Column be involved for the check?

  • Join DataTable activity and postprocessing the Join Result
    OR

LINQ - Match case

(From d in dt1.AsEnumerable
Let c1 = d(0).ToString.ToUpper.Trim
Let chk = dt2.AsEnumerable.Any(Function (d2) d2(0).toString.ToUpper.Trim.Equals()c1)
Where not chk
Select r = d).CopyToDataTable

Handling empty result
:ambulance: :sos: [FirstAid] Handling of The source contains no DataRows exception - News / Tutorials - UiPath Community Forum

we are using any and not where as we can faster stop when first time the condition has been met

@Aswini

dt1.AsEnumerable.Except(dt2.AsEnumerable,system.data.DataRowComparer.Default).CopyToDataTable

Hi @Aswini

Try using if condition

uncommonRows As IEnumerable(Of DataRow) = DT1.AsEnumerable.Except(DT2.AsEnumerable, DataRowComparer.Default)

If uncommonRows.Any()
Then
resultDataTable As DataTable = uncommonRows.CopyToDataTable()
Else
’ Handle the case where there are no uncommon rows

Hope this helps

1 Like

Tried this but getting below error

@Aswini can you show me the syntax you entered

@Dinesh_Guptil FYI i am getting error where there are no rows that are uncommon if DT has got any rows i am not getting error

1 Like

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