【问题标题】:How to Copy contents from one datatable to another datatable multiple times in C#如何在C#中多次将内容从一个数据表复制到另一个数据表
【发布时间】:2020-06-26 05:53:41
【问题描述】:

我想根据 Id 将 Source 表的内容复制到目标表。我想根据 Id 复制源表行。

源表:

Src_Id  Name  age
------------------
0      Abc    20
1      Xyz    21

目标表:

Des_Id     email       Country
--------------------------------
0     xyz@gmail.com   India   
0     abc@gmail.com   USA  
1     gag@gmail.com   Aus
1     ghu@gmail.com   Germany
1     tyu@gmail.com   India

目标表中的预期结果

Des_Id     email       Country    Src_Id  Name  age
------------------------------------------------------
0     xyz@gmail.com   India        0      Abc    20
0     abc@gmail.com   USA          0      Abc    20
1     gag@gmail.com   Aus          1      Xyz    21
1     ghu@gmail.com   Germany      1      Xyz    21
1     tyu@gmail.com   India        1      Xyz    21

【问题讨论】:

    标签: c# asp.net datatable


    【解决方案1】:

    希望对你有更多帮助。

        DataTable dt1 = new DataTable();
        dt1.Columns.Add("Src_Id",typeof(int));
        dt1.Columns.Add("Name",typeof(string));
        dt1.Columns.Add("age", typeof(int));
    
        dt1.Rows.Add(0,"Abc",20);
        dt1.Rows.Add(1, "Xyz", 21);
    
        DataTable dt2 = new DataTable();
        dt2.Columns.Add("Des_Id",typeof(int));
        dt2.Columns.Add("email",typeof(string));
        dt2.Columns.Add("Country", typeof(string));
                 
        dt2.Rows.Add(0,"xyz@gmail.com","India");   
        dt2.Rows.Add(0,"abc@gmail.com","USA");  
        dt2.Rows.Add(1,"gag@gmail.com","Aus");
        dt2.Rows.Add(1,"ghu@gmail.com","Germany");
        dt2.Rows.Add(1, "tyu@gmail.com", "India");
    
        DataTable dtDestination = new DataTable();
        dtDestination.Columns.Add("Des_Id", typeof(int));
        dtDestination.Columns.Add("email", typeof(string));
        dtDestination.Columns.Add("Country", typeof(string));
        dtDestination.Columns.Add("Src_Id", typeof(int));
        dtDestination.Columns.Add("Name", typeof(string));
        dtDestination.Columns.Add("age", typeof(int));
    
    
        var results = from table1 in dt1.AsEnumerable()
                      join table2 in dt2.AsEnumerable() on (int)table1["Src_Id"] equals (int)table2["Des_Id"]
                      select new
                      {
                          Des_Id = (int)table2["Des_Id"],
                          email = (string)table2["email"],
                          Country = (string)table2["Country"],
                          Src_Id = (int)table1["Src_Id"],
                          Name = (string)table1["Name"],
                          age = (int)table1["age"]
                      };
    
        foreach (var item in results)
        {
            dtDestination.Rows.Add(item.Des_Id,item.email,item.Country,item.Src_Id,item.Name,item.age);
        }
    

    【讨论】:

      【解决方案2】:

      我会确保 src_id 是主键,这样 Find 方法才会起作用:

      src.PrimaryKey = new[]{ src.Columns["src_id"] };
      

      然后在 dest 中创建列并使用循环设置值:

      foreach(DataColumn dc in src.Columns)
        dest.Columns.Add(dc.ColumnName, dc.DataType);
      
      foreach(DataRow dr in dest.Rows){
        var x = src.Rows.Find(dr["dest_id"]);
        foreach(DataColumn dc in src.Columns)
          ro[dc.ColumnName] = x[dc.ColumnName];
      }
      

      注意:这里没有任何错误处理(例如找不到源 ID、目标表已经包含名为 x 的列等),但很容易添加。基本前提是我们将 src 中的所有列添加到 dest,然后对于 dest 中的每一行,在 source 中查找该行,并在 source 的列名上使用循环来复制值。如果您确实遇到列名相同但值不同的情况,最简单的解决方案可能是仅更改 src 中列的名称,直到它们在第一个循环中不再相同(添加增量编号),所以循环逻辑仍然有效(两个表的列名需要相同才能使第二个循环工作)。如果您希望采用定位路线,并且 dest 中的列名与 src 不同(例如,在 src 中是 ColumnX,但在 dest 中是 ColumnX1),那么您可以轻松地按数字索引循环列:

        for(int i = 0; i < x.ItemArray.Length; i++)
          ro[(dest.Columns.Count - src.Columns.Count) + i] = x[i];
      

      操作后,dest 表最终成为您想要的数据表。如果你想要一个新的数据表,让 dest 保持不变,你可以先克隆 dest - 虽然我做这种事情时通常会发现这是一个浪费的步骤

      【讨论】:

      • 感谢您的回答,数据表不包含“查找”的定义
      • 啊,对不起..这是一个错字。我习惯于使用强类型数据表。使用datatable.Rows.Find 但请注意,如果您尚未为表声明键,则会收到 MissingPrimaryKey 异常,因此请确保添加一个
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多