Returning the difference between two data tables

Hey group think, me again! In my automation I am trying to compare DT2 to DT1 and return any row that is different from DT1. Row count and columns are static. I just need to make sure no one accidently messed with the static information. Is there a way to do with UiPath activities or will this need to be an invoke code (in which case, what would that be)?

Thanks again!

@jamills
Lets assume:

  • Tables are identical comparable
  • Information of difference count is enough

Give a try on:
Using assign activity:
dt2.AsEnumerable.Except(dt1.AsEnumerable,DataRowComparer.Default).ToList().Count()
will return an integer - count of different rows

Otherwise the join datatable activity can be used to identify matching rows on join columns (inner join)
If count of matching rows s is ame as dt1/2 count, then no differences are found

Other options e.g returning the not matching rows is possible with default uipath activities

Let us know your open questions

Thanks for replying! Indeed to tables should be identical (just validating if someone happened to make a change).

Question… for the assign would it be: (some int32 variable) = dt2.AsEnumerable.Except(dt1.AsEnumerable,DataRowComparer.Default).ToList().Count()?

Would this just tell how many rows are different and not which ones?

@jamills

Question… for the assign would it be: (some int32 variable) = dt2.AsEnumerable.Except(dt1.AsEnumerable,DataRowComparer.Default).ToList().Count()?

Yes, give a try as it is a quick approach
Except is looking on dt2 rows that are not present in dt1 and in this statement will return the count of this evaluation.

https://www.tutorialsteacher.com/linq/linq-set-operators-except

Thanks for the help so far! Is there a way to not get a count, but to store which lines are different?

@jamills
Yes this is possible
dt2.AsEnumerable.Except(dt1.AsEnumerable,DataRowComparer.Default).ToList() will return a List(Of Datarow) result will be remaining dt2 rows after the except with dt1

if there are rows returned we can use CopyToDataTable and will get a datatable with the structure of dt2
if no rows are returned we would get an exception by using CopyToDataTable

A possible flow could look like this:
Assign Activity:
dt2.AsEnumerable.Except(dt1.AsEnumerable,DataRowComparer.Default).ToList()
to: yourDataRowListVar - Datatype: List(Of Datarow)

If Activity:
Condition: yourDataRowListVar.Count > 0
Then: using an assign activity - dtDiff = yourDataRowListVar.CopyToDataTable
Else: using an assign activity - dtDiff = dt2.Clone

Let us know your open questions

2 Likes

Thanks man! This worked like a charm! I clearly need to read up on the power of Enumerables.

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