将一个数据表的列添加到另一个数据表

mar*_*igi 2 c# asp.net datatable

嘿所有人都需要帮助为这个表排序一个循环,似乎无法将一个工作示例应用于模型,无论如何它在这里.

我有2个数据表,每个数据表具有不同的数据和不同的值,唯一的共同点是日期.第一个表包含我想要的所有内容,除了一列值(来自另一个表)所以我需要将此列合并到第一个表,而不是所有其他数据.

理想情况下,我想要的东西看起来像这样:

DataTable tbl1; //Assume both are populated
DataTable tbl2;

tbl1.Columns.Add("newcolumnofdata") //Add a new column to the first table

foreach (DataRow dr in tbl.Rows["newcolumnofdata"]) //Go through each row of this new column
{
    tbl1.Rows.Add(tbl2.Rows["sourceofdata"]); //Add data into each row from tbl2's column.
    tbl1.Columns["date"] = tbl2.Columns["date"]; //The date field being the same in both sources
} 
Run Code Online (Sandbox Code Playgroud)

如果有人可以帮助wud欣赏它,就像我说我只需要一列,我不需要拥有整个其他数据表.干杯.

nWo*_*orx 7

如果第二个表已经包含所有行,但只缺少一个列,则应该足以执行此类操作

DataTable tbl1;
DataTable tbl2;

tbl1.Columns.Add("newCol");

for(int i=0; i<tbl.Rows.Count;i++)
{
   tbl1.Rows[i]["newcol"] = tbl2.Rows[i]["newCol"];
   tbl1.Rows[i]["date"] = tbl2.Rows[i]["date"];
}
Run Code Online (Sandbox Code Playgroud)