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.
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.
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…
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…
Step 4:
Finally, execute the workflow to see the results 🙂
Common Rows:
Uncommon Rows:
Click here to download the Source Code…
Hope it has helped you…