2

我有两张桌子:

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 行的表格。

4

3 回答 3

3

您可以编写自己的比较器来比较 ID

public class IDComparer  : IEqualityComparer<DataRow> 
{   
    public bool Equals(DataRow x, DataRow y)
    {           
        return (int) x["ID"] == (int) y["ID"] ;
    }

    public int GetHashCode(DataRow obj)
    {                           
        return (int) obj["ID"] ;
    }               
}           

然后使用它,

var updatedTable = uCust.Union(cust, new IDComparer ())
于 2013-10-22T09:13:14.607 回答
2

添加主键并使用 Merge 将为您提供答案。

    DataTable customer = new DataTable();
    var customerIdColumn = new DataColumn("ID", typeof(int));
    customer.Columns.Add(customerIdColumn);
    customer.Columns.Add("Address", typeof(string));
    customer.PrimaryKey = new[] { customerIdColumn };

    DataTable updatedCustomer = new DataTable();
    var updatedCustomerIdColumn = new DataColumn("ID", typeof(int));
    updatedCustomer.Columns.Add(updatedCustomerIdColumn);
    updatedCustomer.Columns.Add("Address", typeof(string));
    updatedCustomer.PrimaryKey = new[] { updatedCustomerIdColumn };

    customer.Rows.Add(1, "London, UK");
    customer.Rows.Add(2, "Paris, France");

    updatedCustomer.Rows.Add(1, "Birmingham, UK");


    customer.Merge(updatedCustomer, false);

编辑

要使用 linq 执行此操作,您需要一个 not in。为了清楚起见,我已经把它写得相当冗长。但是,您应该能够很容易地使代码更简洁。

var cust = customer.AsEnumerable();
var uCust = updatedCustomer.AsEnumerable();

var newCust = (from c in cust join u in uCust on c.Field<int>("ID") equals u.Field<int>("ID") select u);
var newUCust =
    (from cu in cust where !(from ucu in uCust select ucu.Field<int>("ID")).Contains(cu.Field<int>("ID")) select cu);
var joined = newCust.Concat(newUCust);
于 2013-10-22T08:34:31.493 回答
1
var query=from c in customer.AsEnumerable()
         join uc in updatedCustomer.AsEnumerable()
         on c.Field<int>("ID") equals uc.Field<int>("ID") into lf
         from uc in lf.DefaultIfEmpty()
         select new
         {
             ID=c.Field<int>("ID"),
             Address=uc==null?c.Field<string>("Address"):uc.Field<string>("Address")
         };

//this will get the result you want,but it is not DataTable.
//you need to convert query to datatable .
DataTable result =customer.Clone();

query.ToList().ForEach(q=>result.Rows.Add(q.ID,q.Address));
于 2013-10-22T08:56:24.087 回答