1
我正在編寫一個使用LINQ連接兩個DataTable的函數,問題是我不提前知道這兩個表包含的列,除了列加入。在加入兩個DataTable時選擇LINQ查詢中的所有可用字段
string id = "ID";
DataTable tableJoined = new DataTable();
tableJoined.Columns.Add(id, typeof(string));
tableJoined.Columns.Add("NAME", typeof(string));
tableJoined.Columns.Add("STOCK", typeof(string));
var result = from dataRows1 in table1.AsEnumerable()
join dataRows2 in table2.AsEnumerable()
on dataRows1.Field<string>(id) equals dataRows2.Field<string>(id)
select tableJoined.LoadDataRow(new object[]
{
dataRows1.Field<string>(id),
dataRows1.Field<string>(1),
dataRows2.Field<string>(1)
}, false);
result.CopyToDataTable();
我可以建立在旅途中,以適應的tableJoined
最終結構的tableJoined
DataTable,並添加列,但我要如何更新LINQ查詢來填充tableJoined
與所有可用字段?現在它只知道表中包含的列數,只填充「ID」,「NAME」和「STOCK」。謝謝。
非常感謝! – Karl