How To Find Common/Uncommon Rows Between Two Datatables – In UiPath

Datatables in programming are commonly used to store the collection of data in rows and columns

We might sometimes need to find the common/uncommon items between the two DataTables.

Let us see how to implement it!!!

Common Values

dt_CommonRows = dt1.AsEnumerable().Intersect(dt2.AsEnumerable(),System.Data.DataRowComparer.Default).CopyToDataTable

UnCommon Values

dt_UnCommonRows = dt1.AsEnumerable().Except(dt2.AsEnumerable(),System.Data.DataRowComparer.Default).CopyToDataTable

Note:
Replace dt1 and dt2 with your appropriate lists

Example

Implementation using UiPath :

Let us implement a workflow that takes a two-sample DataTables and displays the common and uncommon rows between them.

Step 1:
Drag “Build DataTable” activity into the designer panel to build Sample DataTable 1 and initialize it with some sample data as shown below.

Common & Unique Rows Between Two DataTables 1Common & Unique Rows Between Two DataTables 2

Step 2:
Drag “Build DataTable” activity into the designer panel to build Sample DataTable 2 and initialize it with some sample data as shown below.

Common & Unique Rows Between Two DataTables 3Common & Unique Rows Between Two DataTables 4

Step 3:
Drag “Assign” activity into the designer panel and use the below-mentioned code to find the COMMON rows between two DataTables.

dt1.AsEnumerable().Intersect(dt2.AsEnumerable(),System.Data.DataRowComparer.Default).CopyToDataTable

And use “Output DataTable” and the “Message box” to display the resultant DataTable…

Common & Unique Rows Between Two DataTables 5.png

Step 4:
Drag “Assign” activity into the designer panel and use the below-mentioned code to find the UNCOMMON rows between two DataTables.

dt1.AsEnumerable().Except(dt2.AsEnumerable(),System.Data.DataRowComparer.Default).CopyToDataTable

And use “Output DataTable” and the “Message box” to display the resultant DataTable…

Common & Unique Rows Between Two DataTables 6.png

Step 4:
Finally, execute the workflow to see the results 🙂

Common Rows:

Common & Unique Rows Between Two DataTables 7.PNG

Uncommon Rows:

Common & Unique Rows Between Two DataTables 8.PNG

Click here to download the Source Code…

Hope it has helped you…

Leave a Reply

%d bloggers like this: