我有两张桌子:
Customer:
+------------------------------+
| ID | Address |
|------------------------------|
| 1 | London, UK |
| 2 | Paris, France |
+------------------------------+
Updated Customer:
+------------------------------+
| ID | Address |
|------------------------------|
| 1 | Birmingham, UK |
+------------------------------+
如何合并表格以获得此结果?:
Customer:
+------------------------------+
| ID | Address |
|------------------------------|
| 1 | Birmingham, UK |
| 2 | Paris, France |
+------------------------------+
我尝试使用联合的 C#/ Linq 代码:
DataTable customer = new DataTable();
customer.Columns.Add("ID", typeof(int));
customer.Columns.Add("Address", typeof(string));
DataTable updatedCustomer = new DataTable();
updatedCustomer.Columns.Add("ID", typeof(int));
updatedCustomer.Columns.Add("Address", typeof(string));
customer.Rows.Add(1, "London, UK");
customer.Rows.Add(2, "Paris, France");
updatedCustomer.Rows.Add(1, "Birmingham, UK");
var cust = from row in customer.AsEnumerable()
select new
{
ID = row[0],
Address = row[1]
};
var uCust = from row in updatedCustomer.AsEnumerable()
select new
{
ID = row[0],
Address = row[1]
};
var updatedTable = cust.Union(uCust);
//Please use cust and uCust objects, not customer and UpdatedCustomer.
然而,Union 给了我一张包含所有 3 行的表格。