Linq更新并合并两个结果集(来自数据表)

时间:2013-10-22 08:16:17

标签: c# linq

我有两张桌子:

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代码我尝试使用union:

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

3 个答案:

答案 0 :(得分: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 ())

答案 1 :(得分: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执行此操作,您需要不要使用。为了清晰起见,我为此做了相当冗长的事情。但是,你应该可以很容易地使代码更简洁。

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

答案 2 :(得分: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));