如何合并这样的两个DataTable?

时间:2014-06-19 19:02:09

标签: c# system.data.datatable

我有两个数据表

dtTable1:
Name    Id  Contact
Gems    A1  8888880000
George  A2  0000008888

dtTable2:
Col1    Col2    Col3
XXX1    YYY1    ZZZ1
XXX2    YYY2    ZZZ2
XXX3    YYY3    ZZZ3
XXX4    YYY4    ZZZ4

我需要的DataTable是:

Name    Id  Contact         Col1    Col2    Col3
Gems    A1  8888880000  XXX1    YYY1    ZZZ1
George  A2  0000008888  XXX2    YYY2    ZZZ2
null    null   null     XXX3    YYY3    ZZZ3
null    null   null     XXX4    YYY4    ZZZ4

如何使用C#

进行操作

3 个答案:

答案 0 :(得分:0)

DataTable mergeDataTables(DataTable dt1, DataTable dt2)
{
    if(dt1.Rows.Count != dt2.Rows.Count ) throw new Exception();

    var dtResult = new DataTable();

    //add new columns
    foreach (DataColumn col in dt1.Columns)
        dtResult.Columns.Add(col.ColumnName, col.DataType);

    foreach (DataColumn col in dt2.Columns)
        dtResult.Columns.Add(col.ColumnName, col.DataType);

    //fill data
    for (int i = 0; i < dt1.Rows.Count; i++)
    {
        dtResult.Rows.Add(dt1.Rows[i].ItemArray.Concat(dt2.Rows[i].ItemArray).ToArray());
    }

    return dtResult;
}

答案 1 :(得分:0)

你走了。可能不漂亮,但做的工作。 无论哪个DataTable都有更多行,它都会添加它们。

private static DataTable MergeTables(DataTable dt1, DataTable dt2)
{
    DataTable merged = new DataTable();

    //copy column struct from dt1
    merged = dt1.Clone();

    //create columns from dt2
    foreach (DataColumn col in dt2.Columns)
    {
        merged.Columns.Add(col.ColumnName);
    }

    int rows;

    if (dt1.Rows.Count > dt2.Rows.Count)
    {
        rows = dt1.Rows.Count;
    }
    else
    {
        rows = dt2.Rows.Count;
    }

    for (int i = 0; i < rows; i++)
    {
        DataRow row = merged.NewRow();

        if ( i < dt1.Rows.Count)
        {
            for (int c = 0; c < dt1.Columns.Count; c++)
            {
                row[c] = dt1.Rows[i][c];
            }
        }

        if (i < dt2.Rows.Count)
        {
            for (int c2 = dt1.Columns.Count; c2 < dt2.Columns.Count + dt1.Columns.Count; c2++)
            {
                row[c2] = dt2.Rows[i][c2-dt1.Columns.Count];
            }
        }
        merged.Rows.Add(row);
    }
    return merged;
}

答案 2 :(得分:0)

我已经改变了lomed的解决方案。

可能这不是完美的解决方案。但它适用于问题中所需的类似场景:

DataTable MergeDataTables(DataTable dt1, DataTable dt2)
    {
        var dtResult = new DataTable();

        foreach (DataColumn col in dt1.Columns)
            dtResult.Columns.Add(col.ColumnName, col.DataType);

        foreach (DataColumn col in dt2.Columns)
            dtResult.Columns.Add(col.ColumnName, col.DataType);

        //cond: to check which if datatable1 is bigger
        if (dt1.Rows.Count > dt2.Rows.Count)
        {               
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                if (i < dt2.Rows.Count)
                    dtResult.Rows.Add(dt1.Rows[i].ItemArray.Concat(dt2.Rows[i].ItemArray).ToArray());
                else
                {
                    DataRow dr = dtResult.NewRow();
                    foreach (DataColumn col in dt1.Columns)                       
                        dr[col.ColumnName] = dt1.Rows[i][col.ColumnName];
                    dtResult.Rows.Add(dr);
                }                

            }
        }
        //if rows equal or datatable2 is bigger
        else
        {                
            for (int i = 0; i < dt2.Rows.Count; i++)
            {
                if (i < dt1.Rows.Count)
                    dtResult.Rows.Add(dt1.Rows[i].ItemArray.Concat(dt2.Rows[i].ItemArray).ToArray());
                else
                {
                    DataRow dr = dtResult.NewRow();
                    foreach (DataColumn col in dt2.Columns)                                                  
                        dr[col.ColumnName] = dt2.Rows[i][col.ColumnName];
                    dtResult.Rows.Add(dr);
                }
            }                
        }

       return dtResult;
    }