We often come across the need to merge DataTable.
In this blog, we are going to learn how to provide Primary Key to the DataTables and then merge them on the same basis.
DataTables with the following structure:
DataTable1:
| Source | Target | Name |
| 4 | 1 | Article |
| 15 | 2 | Blog |
| 30 | 3 | Code Snippet |
DataTable2:
| Target | Description |
| 1 | This is an article |
| 2 | This is a blog |
| 3 | This is a code snippet |
We want the following output:
Final DataTable:
| Target | Source | Name | Description |
| 1 | 4 | Article | This is an article |
| 2 | 15 | Blog | This is a blog |
| 3 | 30 | Code Snippet | This is a code snippet |
- DataTable1.PrimaryKey = new DataColumn[] { DataTable1.Columns["TARGET"] };
- DataTable2.PrimaryKey = new DataColumn[] { DataTable2.Columns["TARGET"] };
- DataTable1.Merge(DataTable2);

Akshay KokilPosted Aug 4, 2021, 4:54 AM
Very helpful and to the point..Thank You!
maisarah AdamPosted May 29, 2019, 11:00 PM
How about if the primary key is string? can it successful merge
pooja guptaPosted Sep 8, 2015, 3:56 AM
nice