简体   繁体   中英

How to Copy contents from one datatable to another datatable multiple times in C#

I want to copy the contents of Source table to the destination table based on the Id. I want to copy the source table rows based on the Id.

Source Table:

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

Destination Table:

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

Expected Result into Destination table

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

I hope its help you more.

    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);
    }

I'd ensure the src_id is a primary key so the Find method will work:

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

Then create columns in dest and set values with loops:

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];
}

Note: There isn't any error handling (such as source ID not found, dest table already contains a column called x etc) here but it's easy to add. The basic premise is that we add all the columns in src, to dest, then for every row in dest, look up for that row in source and use a loop on source's column names to copy the values across. If you do have a case where the column names are the same but the values differ the simplest solution might be to just change the name of the column in src until they are no longer the same (add an increment number) in the first loop, so the looping logic will still work (both tables need to have columns names the same for the second loop to work). If you instead wish to pursue a positional route, and have the column names in dest differ from src (eg in src it's ColumnX but in dest it's ColumnX1) then you can easily loop the columns by numeral index:

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

After the operation, the dest table ends up being the datatable you wanted. If you want a new datatable, leaving dest unmodified, you can clone dest first - I usually find it a wasted step when I do this kind of thing though

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM