两个DataTable与不同的PrimaryKey集合并

时间:2014-04-02 11:30:06

标签: c# .net datatable merge

我有两个数据表如下

TableA
-------------------------------------------
ParamId   | Code   | Val   | date_time
-------------------------------------------
1         A1       5.6     02.04.2014 00:00
1         A1       10      02.04.2014 01:00
1         A1       5.9     02.04.2014 02:00
1         A1       8       02.04.2014 03:00
1         A1       7       02.04.2014 04:00
1         A1       12      02.04.2014 05:00


TableB
-------------------------------------------
ParamId   | Code   | Val   | date_time
-------------------------------------------
1         A2       5.6     02.04.2014 00:00
1         A2       10      02.04.2014 01:00
1         A2       5.9     02.04.2014 02:00
1         A2       8       02.04.2014 03:00
1         A2       7       02.04.2014 04:00
1         A2       12      02.04.2014 05:00

Expected result:
-------------------------------------------
ParamId   | Code   | Val   | date_time
-------------------------------------------
1         A1       5.6     02.04.2014 00:00
1         A1       10      02.04.2014 01:00
1         A1       5.9     02.04.2014 02:00
1         A1       8       02.04.2014 03:00
1         A1       7       02.04.2014 04:00
1         A1       12      02.04.2014 05:00
1         A2       5.6     02.04.2014 00:00
1         A2       10      02.04.2014 01:00
1         A2       5.9     02.04.2014 02:00
1         A2       8       02.04.2014 03:00
1         A2       7       02.04.2014 04:00
1         A2       12      02.04.2014 05:00

即。 TableA具有来自列dt的PrimaryKey,结果表具有来自列dt和Code的primaryKey。 我尝试使用TableA.Merge(TableB);比循环中的ResultTable.Merge(TableA)。但在ResultTable中,我只有最后合并的表值。

1 个答案:

答案 0 :(得分:1)

您需要定义一个空的合并目标表,并将所有其他表合并到该合并表中。所以不要提前合并任何表格。您的代码看起来像这样,数据表merged保存最终结果。在Merge方法中,我使用MissingSchemaAction.Ignore来防止由于提供的数据集中的主键列之间可能不匹配而导致的nullreferenceexception。

var one = BuildDataTable();
one.PrimaryKey = new DataColumn[] { one.Columns["code"]}; 

var row = one.NewRow();
row["id"] = "1";
row["code"] = 1;
row["val"] = 5.6;
row["date_time"] = DateTime.Now;
one.Rows.Add(row);


var two = BuildDataTable();
two.PrimaryKey = new DataColumn[] { two.Columns["date_time"] , two.Columns["code"]};

row = two.NewRow();
row["id"] = "1";
row["code"] = 2;
row["val"] = 3.0;
row["date_time"] = DateTime.Now.AddDays(1);
two.Rows.Add(row);

// merge table result
var merged = BuildDataTable();
merged.PrimaryKey = new DataColumn[] { 
                        merged.Columns["date_time"] , 
                        merged.Columns["code"]};

// for each table call merge on our merged table
merged.Merge(one, true, MissingSchemaAction.Ignore); 
merged.Merge(two, true, MissingSchemaAction.Ignore);
// continue calling Merge until all tables are done

构建表的辅助方法

private DataTable BuildDataTable()
{
   var dt = new DataTable();
    //ParamId   | Code   | Val   | date_time
    dt.Columns.Add("id", typeof(string));
    dt.Columns.Add("code", typeof(int));
    dt.Columns.Add("val", typeof(double));
    dt.Columns.Add("date_time", typeof(DateTime));
    return dt;
}