![](/img/trans.png)
[英]How to copy data from one column to another at the same datatable in c#
[英]How to Copy contents from one datatable to another datatable multiple times in C#
我想根據 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
我希望它對你有更多幫助。
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);
}
我會確保 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 - 雖然我做這種事情時通常會發現這是一個浪費的步驟
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.