Merge datatables with nothing in common without using for each

Hi,

I’m trying to merge two data tables, I have many columns in both so I was trying to not use for each, get row item and add data row. I’ve tried merge and join, on merge it “jumps” one row and leave it empty, and with join it puts two equal columns on data table.

I just want to do that. Example:

DT 1
Column A | Column B
1 | 2

DT 2
Column C | Column D
3 |4

dtMerged
Column A | Column B | Column C | Column D

1 | 2 | 3 | 4

Regards,

Alamyr

Do the 2 datatables have the exact number of rows? The tables need something in common to merge them, otherwise how will the system know which row goes where in the newly created datatable?

This can be done with the rowindex if you don’t join on a specific column, which is what it sounds like you want to do, assuming there are the same number of rows. This should happen automatically when using the Merge datatable method and indicate that you want the missing schema to be added. See more documentation here: https://docs.microsoft.com/en-us/dotnet/api/system.data.datatable.merge

Yes, they have the same number of rows. But I didn’t understand how to do it on uipath. Could you give me one example?

Using the merge datatable it’s not possible because it divide the data on two rows

Column A | Column B |Column C | Column D

1 |2 | (blank)| (blank)
(blank)|blank)| 3 | 4

I apologize, I thought the merge method would add the rowindex automatically, but you have to do that yourself manually.

‘add datacolumn activity’ do this twice - once for dt1 and once for dt2. Add a column called RowIndex1 for dt1, and RowIndex2 for dt2

For each row in dt1 // Be sure to use the output in the properties pane and create an int32 variable called CurrentRowIndex
Assign row.item(“RowIndex1”) = CurrentRowIndex

Now repeat the for each row for dt2

Use the Join Datatable Activity to inner join dt2 to dt1 on dt1(“RowIndex1”) = dt2(“RowIndex2”) and save as dtMerged

‘Remove datacolumn activity’ - use 2 of these to remove the “RowIndex1” and “RowIndex2” columns from dtMerged

Now you have a merged datatable. Note that if you have different amount of rows between the datatables, any extra from dt2 will be excluded, and any extra from dt1 will have the dt2 columns be null.

NOTE: You could do this with LINQ instead which would remove some of the steps of adding/removing columns, but this way uses all built-in activities which can be much easier to maintain for people who are new to .NET

Here is the .xaml doing what I mentioned above: delete4.xaml (12.2 KB)

1 Like

Thank you very much for the help, Dave!

1 Like