簡體   English   中英

使用linq合並兩個DataTable的列

[英]Merge columns of two DataTables using linq

我有兩個DataTabledt1dt2

dt1

ID     | Name     | Address | QTY
-------+----------+---------+-----
A1     | Dog      | C1      | 272    
A2     | Cat      | C3      | 235    
A3     | Chicken  | C2      | 254    
A4     | Mouse    | C4      | 259 
A5     | Pig      | C5      | 233 

dt2

ID     | Name     | Address | QTY MAX
-------+----------+---------+--------
A1     | Dog      | C1      | 250    
A2     | Cat      | C3      | 200    
A3     | Chicken  | C2      | 300    
A6     | Rabbit   | C6      | 350    

但是,我想合並dt1dt2dt3象下面這樣:

ID     | Name     | Address | QTY   | QTY MAX
-------+----------+---------+-------+--------
A1     | Dog      | C1      | 272   | 250
A2     | Cat      | C3      | 235   | 200
A3     | Chicken  | C2      | 254   | 300
A4     | Mouse    | C4      | 259   | 0
A5     | Pig      | C5      | 233   | 0
A6     | Rabbit   | C6      | 0     | 350

誰能幫我?

該解決方案不是解決方案,因為您可以簡單地使用DataTable.MergeDataTable.PrimaryKey來獲取所需的輸出。

這是一個可以使用的虛擬示例:

var dt1 = new DataTable();
var p1 = dt1.Columns.Add("a", typeof(int)); //Use this to add Primary Key constraint
dt1.Columns.Add("b");
dt1.Columns.Add("c");
dt1.Rows.Add("1", "apple", "10");
dt1.Rows.Add("2", "mango", "20");
dt1.Rows.Add("3", "orange", "30");
dt1.Rows.Add("4", "banana", "40");
dt1.PrimaryKey = new DataColumn[] { p1 }; //This removes duplication of rows

var dt2 = new DataTable();
var p2 = dt2.Columns.Add("a", typeof(int)); //Use this to add Primary Key constraint        
dt2.Columns.Add("b");
dt2.Columns.Add("d");
dt2.Rows.Add("1", "apple", "50");
dt2.Rows.Add("2", "mango", "60");
dt2.Rows.Add("3", "orange", "70");
dt2.Rows.Add("5", "grapes", "80");
dt2.PrimaryKey = new DataColumn[] { p2 }; //This removes duplication of rows

var dt3 = dt1.Copy();
dt3.Merge(dt2);  // Merge here merges the values from both provided DataTables

考慮到您的問題:

var dt1 = new DataTable();
var p1 = dt1.Columns.Add("ID", typeof(string));
dt1.Columns.Add("Name", typeof(string));
dt1.Columns.Add("Address", typeof(string));
dt1.Columns.Add("Qty", typeof(int));
dt1.Columns["Qty"].DefaultValue = 0; //Setting default value
dt1.Rows.Add("A1", "Dog", "C1", 100);
dt1.Rows.Add("A2", "Cat", "C3", 200);
dt1.Rows.Add("A3", "Chicken", "C2", 300);
dt1.Rows.Add("A4", "Mouse", "C4", 400);
dt1.Rows.Add("A5", "Pig", "C5", 500);
dt1.PrimaryKey = new DataColumn[] { p1 };

var dt2 = new DataTable();
var p2 = dt2.Columns.Add("ID", typeof(string));
dt2.Columns.Add("Name", typeof(string));
dt2.Columns.Add("Address", typeof(string));
dt2.Columns.Add("Qty Max", typeof(int));
dt2.Columns["Qty Max"].DefaultValue = 0; //Setting default value
dt2.Rows.Add("A1", "Dog", "C1", 600);
dt2.Rows.Add("A2", "Cat", "C3", 700);
dt2.Rows.Add("A3", "Chicken", "C2", 800);
dt2.Rows.Add("A6", "Rabbit", "C6", 900);
dt2.PrimaryKey = new DataColumn[] { p2 };

var dt3 = dt1.Copy();
dt3.Merge(dt2);

輸出:

在此處輸入圖片說明


感謝@ shA.t建議包含DataColumn.DefaultValue以便可以將空白單元格替換為0 他的答案似乎還包括功能,我想這就是您要尋找的功能!

如果您的DataTable沒有主鍵,並且您不能或不想更改這些DataTable ,則可以使用如下代碼:

// At first you need to define your result `DataTable`
// So make it by cloning from first `DataTable`
var dt3 = dt1.Clone();  
// Then add extra columns to it
dt3.Columns.Add("Qty Max", typeof(int));

// Second, you need to add rows of first `DataTable`
foreach (DataRow row in dt1.Rows)
{
    // When you don't have a primary key you need a code like this to find same rows:
    var dt2Row = dt2.Rows.OfType<DataRow>().SingleOrDefault(w => w["ID"].Equals(row["ID"]));
    var qtyMax = dt2Row?["Qty Max"] ?? 0;    // Here I set default value to `0`

    dt3.Rows.Add(row["ID"], row["Name"], row["Address"], row["Qty"], qtyMax);
}

// Third, you need to add rows of second `DataTable` that is not in first
var dt2OnlyRows =
    dt2.Rows.OfType<DataRow>().Where(w => dt1.Rows.OfType<DataRow>().All(x => x["ID"] != w["ID"]));
foreach (var row in dt2OnlyRows)
{
    dt3.Rows.Add(row["ID"], row["Name"], row["Address"], 0, row["Qty Max"]);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM