by
3,709 13
0
4
12k
7
Top 1% !
Pearl of Wisdom
Specified
OpenSource
Popularity: 339th place
No tags for this snippet yet.
.NETFramework4.5
Languagec#

Join Two Datatables

also deals with duplicate field names
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/1571178/Join-Two-Datatables?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
DataTable targetTable = dataTable1.Clone(); var dt2Columns = dataTable2.Columns.OfType<DataColumn>().Select(dc => new DataColumn(dc.ColumnName, dc.DataType, dc.Expression, dc.ColumnMapping)); var dt2FinalColumns=from dc in dt2Columns.AsEnumerable() where targetTable.Columns.Contains(dc.ColumnName) == false select dc; targetTable.Columns.AddRange(dt2FinalColumns.ToArray()); var rowData =from row1 in dataTable1.AsEnumerable() join row2 in dataTable2.AsEnumerable() on row1.Field<int>("ID") equals row2.Field<int>("ID") select row1.ItemArray.Concat(row2.ItemArray.Where(r2=> row1.ItemArray.Contains(r2)==false)).ToArray(); foreach (object[] values in rowData) targetTable.Rows.Add(values);
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus