# Join Two DataTables

**URL:** <https://forum.uipath.com/t/join-two-datatables/87>\
**Category:** Help\
**Tags:** datatable, system, activities\
**Created:** [October 21, 2016, 1:54pm UTC](https://forum.uipath.com/t/join-two-datatables/87 "2016-10-21T13:54:53Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![UiRobot](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/uirobot/32/11_2.png) [@UiRobot](https://forum.uipath.com/u/UiRobot)\
**Post date:** [October 21, 2016, 1:54pm UTC](https://forum.uipath.com/t/join-two-datatables/87/1 "2016-10-21T13:54:53Z")

</div>

Trevor Napier  
June 27, 2016 21:44 NONE   
So I know going through VB/C# you can use LINQ to join two tables IE

Dim query = From tl1 In InputTableOne.AsEnumerable() \_  
Join tl2 In InputTableTwo.AsEnumerable() \_  
On tl1.Field(Of Integer)(“Customer\_ID”) Equals \_  
tl2.Field(Of Integer)(“Customer\_ID”) \_  
Select New With \_  
{ \_  
.ID = tl1.Field(Of Integer)(“Customer\_ID”), \_  
.Customer\_Name = tl1.Field(Of String)(“Customer\_Name”), \_  
.Order\_ID = tl2.Field(Of Integer)(“Order\_ID”), \_  
.Order\_Item = tl2.Field(Of String)(“Item”) \_  
};

Then loop over the results to add/build the joined table.

Is this possible within UiPath? I have tried several ways but always seem to hit type errors at the loop. Am I missing some other way to do this?

---

<div class="post-metadata">

**Author:** ![UiRobot](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/uirobot/32/11_2.png) [@UiRobot](https://forum.uipath.com/u/UiRobot)\
**Post date:** [October 21, 2016, 1:55pm UTC](https://forum.uipath.com/t/join-two-datatables/87/2 "2016-10-21T13:55:04Z")

</div>

Alex Vasile June 30, 2016 02:55 Official comment  
Hello Trevor,

Let’s assume that you have two DataTables. In order to do an inner join you can follow these steps:

1. Declare a third DataTable and into an Assign activity copy the structure from one of the first two using the Clone method.

2. Use two For each row activities to loop through the DataTables.

3. With an If activity, check this condition:

Enumerable.SequenceEqual(row.ItemArray,row2.ItemArray)

1. If the condition is true, then you can use an Invoke Method activity where you need to provide three properties:

-TargetObject: DataTable3.Rows

-MethodName: Add

-Parameters: An In parameter of Type:Object and Value: row.ItemArray

I hope these instructions will help you.

---

<div class="post-metadata">

**Author:** ![UiRobot](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/uirobot/32/11_2.png) [@UiRobot](https://forum.uipath.com/u/UiRobot)\
**Post date:** [October 21, 2016, 1:55pm UTC](https://forum.uipath.com/t/join-two-datatables/87/3 "2016-10-21T13:55:13Z")

</div>

Trevor Napier July 07, 2016 15:57  
I ended up solving this problem by creating a plug in to join tables based on shared columns/keys.

I did try this method as well which will give a final result set of rows that were the same in both tables, but does not actually perform a join of the data.

---

<div class="post-metadata">

**Author:** ![tobor](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@tobor](https://forum.uipath.com/u/tobor)\
**Post date:** [April 20, 2017, 2:52pm UTC](https://forum.uipath.com/t/join-two-datatables/87/4 "2017-04-20T14:52:45Z")

</div>

Hi Alex. I am trying to use this method to join 2 tables but am not familiar with “loop through”. Is it possible to explain this further and, even better, provide a simple example xaml file.

Thanks in advance

---

<div class="post-metadata">

**Author:** ![aksh1yadav](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/aksh1yadav/32/135359_2.png) [@aksh1yadav](https://forum.uipath.com/u/aksh1yadav)\
**Post date:** [April 20, 2017, 3:20pm UTC](https://forum.uipath.com/t/join-two-datatables/87/5 "2017-04-20T15:20:33Z")

</div>

> [@tobor](#):
>
> I am trying to use this method to join 2 tables

You can use **Merge Datatable Activity** here 🙂

For your reference:

> [@How to merge two data tables](https://forum.uipath.com/t/how-to-merge-two-data-tables/2223/8):
>
> hey @Sunitha_Bist we can merge two tables into a single table but you should know some points before merging: \*\*If the number of columns do not match the second table\* When the number of columns do not match the second table then it creates blank columns for the table for the column(s) that do not match, as in the following: If the data type of a column does not match the second table The data types must match. If the column names are the same and if the column name in both tables are th…

Regards…!!  
Aksh

---

<div class="post-metadata">

**Author:** ![tobor](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@tobor](https://forum.uipath.com/u/tobor)\
**Post date:** [April 20, 2017, 3:26pm UTC](https://forum.uipath.com/t/join-two-datatables/87/6 "2017-04-20T15:26:10Z")

</div>

Hi Aksh. I am looking to join 2 tables to extend the data in each row using a field common to each table (similar to vlookup). Is that possible using merge? I thought that merge would take 2 tables with the same (or similar) field structures and add the rows, e.g. one table with 10 rows and the other table with 15 would output a new table with 25 rows.

---

<div class="post-metadata">

**Author:** ![aksh1yadav](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/aksh1yadav/32/135359_2.png) [@aksh1yadav](https://forum.uipath.com/u/aksh1yadav)\
**Post date:** [April 20, 2017, 3:28pm UTC](https://forum.uipath.com/t/join-two-datatables/87/7 "2017-04-20T15:28:05Z")

</div>

Yeah i have mentioned it in the post already if you have different then you can use For-each construct or For-each row activity and add new data to a new datatable using Add Data row activity.

> [@tobor](#):
>
> . I am looking to join 2 tables to extend the data in each row using a field common to each table

You means if suppose same email field found in both then just skip that row or if not present then just add it ? right?

Regards…!!  
Aksh

---

<div class="post-metadata">

**Author:** ![tobor](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@tobor](https://forum.uipath.com/u/tobor)\
**Post date:** [April 20, 2017, 3:47pm UTC](https://forum.uipath.com/t/join-two-datatables/87/8 "2017-04-20T15:47:40Z")

</div>

No. I don’t want to merge the tables, I want to join them. For example consider these 2 tables:

table 1:  
Index,First\_Name  
1,john  
2,james  
3,fred  
4,alan

table 2  
Index,Second\_Name  
1,smith  
2,jones  
3,hunter  
4,evans

I want to create table 3:

Index,First\_Name,Second\_Name  
1,john,smith  
2,james,jones  
3,fred,hunter  
4,alan,evans

Can this be done using merge?

---

<div class="post-metadata">

**Author:** ![aksh1yadav](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/aksh1yadav/32/135359_2.png) [@aksh1yadav](https://forum.uipath.com/u/aksh1yadav)\
**Post date:** [April 20, 2017, 4:08pm UTC](https://forum.uipath.com/t/join-two-datatables/87/9 "2017-04-20T16:08:35Z")

</div>

No Merge will create the second row if you will use index also on datatable so it will treat as a primary key.

so you can get some idea from here:

> [@How to merge two data tables](https://forum.uipath.com/t/how-to-merge-two-data-tables/2223/14):
>
> @Sunitha_Bist My idea is similar to the attached code. You can tweak it a bit as per your requirement. [merge.zip](https://global.discourse-cdn.com/uipath/original/2X/b/bfff724097b93af42bd8db5886df92db441fea01.zip) (19.9 KB)

let me know if still face some issues 🙂

Regards…!!  
Aksh

---

<div class="post-metadata">

**Author:** ![tobor](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@tobor](https://forum.uipath.com/u/tobor)\
**Post date:** [April 20, 2017, 4:17pm UTC](https://forum.uipath.com/t/join-two-datatables/87/10 "2017-04-20T16:17:50Z")

</div>

Thank you. I will look at the example you sent.

---

<div class="post-metadata">

**Author:** ![tobor](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@tobor](https://forum.uipath.com/u/tobor)\
**Post date:** [April 20, 2017, 4:32pm UTC](https://forum.uipath.com/t/join-two-datatables/87/11 "2017-04-20T16:32:56Z")

</div>

Hi Aksh. Your example does not join the tables based on a common field between table 1 and 2. The output is:

Name City Country  
abc xxx abcd  
def yyy efgh  
xx xxx   
pp pppp   
sss sssss

However, there is no logical join explaining why city xxx is in country abcd.

Alex Vaslie’s solution is the best but I need help understanding it. Can you create an example in line with Alex’s solution?

---

<div class="post-metadata">

**Author:** ![aksh1yadav](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/aksh1yadav/32/135359_2.png) [@aksh1yadav](https://forum.uipath.com/u/aksh1yadav)\
**Post date:** [April 20, 2017, 4:43pm UTC](https://forum.uipath.com/t/join-two-datatables/87/12 "2017-04-20T16:43:18Z")

</div>

That was just for your guidance 😉 using loop. 😛

> [@tobor](#):
>
> Alex Vaslie’s solution is the best but I need help understanding it. Can you create an example in line with Alex’s solution?

Will do, Once will sit on my system… On Mobile. Can not use UiPath on it 😛

Regards…!!  
Aksh

---

<div class="post-metadata">

**Author:** ![vvaidya](https://avatars.discourse-cdn.com/v4/letter/v/e56c9b/32.png) [@vvaidya](https://forum.uipath.com/u/vvaidya)\
**Post date:** [April 20, 2017, 4:55pm UTC](https://forum.uipath.com/t/join-two-datatables/87/13 "2017-04-20T16:55:14Z")

</div>

@tobor Check the attached solution. Hope it helps.

[JoinTables.xaml](https://global.discourse-cdn.com/uipath/original/2X/7/75081004b78d00c5d5812ea76bac27e8c0d6b18b.xaml) (10.4 KB)

---

<div class="post-metadata">

**Author:** ![tobor](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@tobor](https://forum.uipath.com/u/tobor)\
**Post date:** [April 20, 2017, 6:01pm UTC](https://forum.uipath.com/t/join-two-datatables/87/14 "2017-04-20T18:01:33Z")

</div>

That’s great. Thank you @vvadiya.

Thank you also to @aksh1yadav

---

<div class="post-metadata">

**Author:** ![tobor](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@tobor](https://forum.uipath.com/u/tobor)\
**Post date:** [April 24, 2017, 4:34pm UTC](https://forum.uipath.com/t/join-two-datatables/87/15 "2017-04-24T16:34:43Z")

</div>

Having joined 2 tables, I now have some rows with blank fields (because the join field is only present in 1 table). I have tried to filter and remove rows based on the blank fields but this is not working. How can the output table best be filtered to include rows for which join data is available in both tables?

---

<div class="post-metadata">

**Author:** ![jcarr79](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/jcarr79/32/7812_2.png) [@jcarr79](https://forum.uipath.com/u/jcarr79)\
**Post date:** [October 10, 2017, 3:02pm UTC](https://forum.uipath.com/t/join-two-datatables/87/16 "2017-10-10T15:02:26Z")

</div>

I’m working on something similar and I cannot seem to get this to work. I’ve tried both the default Merge DataTable activity and your Invoke Method version, and I am having the same issue in both cases - the rows from the second datatable are being appended rather than merged with the rows with the same primary key from the first datatable.

In my case, the datatables in question are being generated by the Excel Read Range activity to pull from two different worksheets, but I have confirmed that the Excel formatting is the same, and the columns are successfully merging where applicable, just not the rows with the same primary key.

---

<div class="post-metadata">

**Author:** ![vvaidya](https://avatars.discourse-cdn.com/v4/letter/v/e56c9b/32.png) [@vvaidya](https://forum.uipath.com/u/vvaidya)\
**Post date:** [October 10, 2017, 3:38pm UTC](https://forum.uipath.com/t/join-two-datatables/87/17 "2017-10-10T15:38:38Z")

</div>

Can you share your workflow?

---

<div class="post-metadata">

**Author:** ![jcarr79](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/jcarr79/32/7812_2.png) [@jcarr79](https://forum.uipath.com/u/jcarr79)\
**Post date:** [October 10, 2017, 7:28pm UTC](https://forum.uipath.com/t/join-two-datatables/87/18 "2017-10-10T19:28:22Z")

</div>

PM’d it to you

---

<div class="post-metadata">

**Author:** ![GERMAN\_LARRAIN](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/german_larrain/32/61991_2.png) [@GERMAN\_LARRAIN](https://forum.uipath.com/u/GERMAN_LARRAIN)\
**Post date:** [February 6, 2018, 1:48pm UTC](https://forum.uipath.com/t/join-two-datatables/87/19 "2018-02-06T13:48:35Z")

</div>

Nice example.

2 improvements if anyone can tell me:

1. When i declare the PrimaryKey; and so the key has already repeated values on the column. How can i previously detect and delete them (with a rule like keeping the first or the more fullfilled row) (I was planning a double for to check everything, but to hard with plenty of rows)

2. I whant to do a LEFT join, so if the 2nd Datatable has extra values of keys that doesn’t appear on the 1st DT, don’t include that rows on (i was planning to iterate and clean those, but maybe you have a better efficient idea. Also a DT.Select could be)

---

<div class="post-metadata">

**Author:** ![krisleunis](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/krisleunis/32/30046_2.png) [@krisleunis](https://forum.uipath.com/u/krisleunis)\
**Post date:** [September 14, 2018, 6:49am UTC](https://forum.uipath.com/t/join-two-datatables/87/20 "2018-09-14T06:49:29Z")

</div>

Hi all, is there an efficient way to outer join 2 data tables (without looping through the datatables)?

For example consider below 2 datatables as input:

dtInputOne  
ID,Value  
1,“John”  
2,“Ringo”  
3,“Paul”  
4,“George”

dtInputTwo  
ID,Value  
1,“John”  
2,“Ringo”  
3,“Paul”  
4,“George”  
5,“Yoko”

Based on these, how can I obtain a third datatable “dtOutput”?

dtOutput  
ID,Value  
5,“Yoko”

It appears in UiPath Studio 2018.3.0 Beta the Join Data Tables does not contain an outer join option.  
Thanks!

Kind regards,

Kris

[Next page](https://forum.uipath.com/t/join-two-datatables/87.md?page=2)
