Click here to Skip to main content
15,887,881 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a two dattables

Dt1 and Dt2

Dt1

Id | Name
1 | a
2 | b

Dt2

age | city
11 | kochi
21 | Bangalore

I want the out put like

Id | Name |age |city
1 | a | 11 | kochi
2 | b | 21 | Bangalore

What I have tried:

I have tried this

Dt1= Dt1.Merge(Dt2);
Posted
Updated 11-May-18 4:20am

The problem is that Merge doesn't do what you want: it appends one table to another with a similar schema.
So Merge will give you this:
1   a
2   b
11  Kochi
21  Bangalore
It can't "combine columns" to form a new table, because it has no idea how the rows are related - and in fact there is nothing in your tables which in any way relates two rows together.

To do what you want, you will have to create a new third table with four columns, and manually add the data to each row. (And hope like heck they have the same number of rows).

At a guess, your data organisation is flawed, and needs some serious looking at instead of the "combine" operation you are trying for.
 
Share this answer
 
Another option would be to add a column to DT2 as ID, and have that ID equal to the corresponding line in DT1. This will define the relationship between the tables, and these can be joined utilizing LINQ.

Stack Overflow => Inner Join of DataTables in C#
[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900