Compare Two Datatables by a column value and need to update to second Datatable if there is any changes available on the Master Datatable

Hi,

I have two Datatables(Master Datatable and second Datatable), I want to compare both datatable by a column name(Emp_Id) and need to update the second datatable if there is any changes available on the Master Datatable. can anyone help me on this?

For Example,
Below is the Master Datatable,

Emp_id Name Email phno Location
10011 A Abcd@gmail.com 9876543210 Chennai
10012 B B@gmail.com 7894561230 Madurai
10013 C C@gmail.com 4561230789 Thanjavur
10014 D D@gmail.com 9632587410 Trichy

And the Second Datatable is,

Emp_id Name Email phno Location
10011 A A@gmail.com 9876543210 Chennai
10012 B B@gmail.com 7894561230 Madurai
10013 C C@gmail.com 4561230789 Salem
10014 D D@gmail.com 9632587410 Trichy

From the Above example, Mail id needs to update to the second datatable for Emp_id 10011 and Location needs to update to the second datatable for Emp_id 10013.

Thanks in Advance!!

Hi @Abirami_Anbukumar ,

Could you try to use Join Datatables Activity and Check ?

The Following Steps should help you in achieving the result :

  1. After Reading the Excel Sheets as Datatables, we keep the First Datatable as Input Datatable 1 and Second Datatable as Input Datatable 2 in the Join Datatables Activity. We Select Inner Join as the Join Type. We Declare and create an Output Datatable. The Columns to Join as Emp_id.
    image

  2. Now the Output Datatable will produce the Column Names containing both the Input Datatables, as they have the same column names they will have a suffix added to them as _1. We would require to either remove the First Datatable Column Names and keep only the Column Names ending with _1, as they are of the Second Datatable.
    This Should be the Output required, although the column names are different, we can then Change it to the desired Column Name.

Keeping only the Required Column Names :

OutputDT.DefaultView.ToTable(false,"Emp_id","Name_1","Email_1","phno_1","Location_1")

image

Changing the Column Names to the Original Names :
image

For a Quick Check , Just Perform the Joining and Check the Output Datatable and Check if it contains the Data Required as the Output.

Let us know if this doesn’t work. We could also try with other approaches.

Hi @supermanPunch ,

Thanks for your help!!!:slight_smile:

After Comparing both datatable with Emp_id, it should find the changes specifically(Name Or Mail Or phno Or location) in Master Datatable based on Column value(Emp_id) and update the changes in the second Datatables instead of updating the whole data.

And also need to update to Second DataTable if there is any new Emp_id available in the Master DataTable.

@Abirami_Anbukumar ,

Let’s Consider the Below Datatable as Master Datatable :
image

Let the Datatable below be the Second Datatable :
image

As you can notice, in the Master Data, there is an explicit row with Emp_id 1015. In the Second Datatable, there is an Explicit row with Emp_id 1016. Hence, the Resultant Datatable as per the Requirement Should appear as below :
image

Resultant Datatable = Updated Values from Master DT + New Rows from Master DT (Not available in Second DT, Emp_id 1015) + Not Matching Rows from Second DT (Emp_id 1016)

If the above is the Output Expected, Check the Workflow Below :
DT_Update_Values.xaml (16.4 KB)

Hello @Abirami_Anbukumar ,

Check whether the below video can help you. It will help to get the similar and dissimilar rows from both the excels and once it identifies the dissimilar rows, you can loop through it and update the value.

Instead of checking one by one, you can get the dissimilar list and do the verification.

Maybe the case can be based on another interpretation (kindly note: join vs. match vs. lookup)
We can also wotk with SequenceEquals but in general we can state:

  • when a matching EMPID is within the second dt, then use this row column values
  • when not a matching row is found in dtsecond for a dtmaster row, use dtmaster row column values

So we can simplify it to following:
grafik


(From dleft In dtLeft.AsEnumerable
Let dr = dtRight.AsEnumerable.Where(Function (x) x("Emp_id").toString.Trim.Equals(dLeft("Emp_id").toString.Trim)).DefaultIfEmpty(dleft).First()
Select r=dr).CopyToDataTable

grafik

Find starter help here:
Match_1Col_AllColLeftFromRight_WhenAnyRColDiffers.xaml (12.8 KB)

1 Like