If a
LINQ
query is required to merge the
DataColumns
"Id" and "Name"
of a
DataTable
to get another
DataTable
with single
DataColumn
, then the following code can be used.
void Main()
{
DataTable table1 = new DataTable();
table1.Columns.Add("Id",typeof(string),null);
table1.Columns.Add("Name",typeof(string),null);
table1.Rows.Add("1","Name one");
table1.Rows.Add("2","Name two");
table1.Rows.Add("3","Name three");
DataTable table2 = new DataTable();
table2.Columns.Add("IdName",typeof(string),null);
table1.AsEnumerable().Select (
row => {DataRow mRow = table2.NewRow();
mRow["IdName"]= string.Format("{0}: {1}",
row.Field<string>("Id"),
row.Field<string>("Name"));
return mRow;}).CopyToDataTable(table2,
LoadOption.OverwriteChanges);
}