c#根据键将列表与另一个列表合并

时间:2014-05-22 19:17:33

标签: c# linq list merge

我的主要对象是客户列表。由于我必须获取数据的方式,我有两个列表需要合并,如下所示。余额清单包括所有客户的余额。基本上我想将与特定客户相关的余额添加到客户列表中。我试图使用Linq根据CustomerNumber的共同密钥合并两者,但是还没有能够使它工作。

List<Customer> customers
List<Balance> balances

public class Customer
{
     int CustomerNumber {get;set;}
     List<Balance> Balances{get;set;}
}

public class Balance
{
     int CustomerNumber {get;set;}
     decimal Amount {get;set;}
}

所以,请保存我有这个客户列表

List<Customer> customers = new List<Customer>{
new Customer {CustomerNumber = 1},
new Customer {CustomerNumber = 2},
new Customer {CustomerNumber = 3}
};

此余额清单

List<Balance> balances = new List<Balance>{
new Balance {Amount = 10, CustomerNumber = 1},
new Balance {Amount = 20, CustomerNumber = 1},
new Balance {Amount = 30, CustomerNumber = 2},
new Balance {Amount = 80, CustomerNumber = 2},
new Balance {Amount = 100, CustomerNumber = 2},
new Balance {Amount = 50, CustomerNumber = 3},
};

最后我会

  • 客户1的列表中包含 10 20 金额
  • 客户2的列表中包含 30 80 100 金额
  • 客户3的列表中包含 50 金额。

目前的解决方案如下:

foreach (var customer in customers)
{
    var matchingBalances = balances
                     .Where(x => x.CustomerNumber == customer.CustomerNumber);
    customer.Balances.AddRange(matchingBalances);
}

更新:这就是我最终使用的内容。

var merge2 = customers.GroupJoin(balances,
        c => c.CustomerNumber,
        b => b.CustomerNumber,
        (c, b) =>
        {                
            c.Balances = b.ToList()
            return c;
        });

3 个答案:

答案 0 :(得分:1)

尝试GroupJoin ...

这将是金额......

var merge = customers.GroupJoin(balances, 
            c => c.CustomerNumber, 
            b => b.CustomerNumber,
            (c, b) => new
            {
                custname = c.CustomerNumber, 
                custamount = b.Sum(b2 => b2.Amount)
            });

            foreach (var cust in merge)
            {
                Console.WriteLine("{0}: {1}", cust.custname, cust.custamount);
            }

以下将列出它们....

        var merge2 = customers.GroupJoin(balances,
            c => c.CustomerNumber,
            b => b.CustomerNumber,
            (c, b) => new
            {
                custname = c.CustomerNumber,
                custamount = b.ToList()
            });

        foreach (var cust in merge2)
        {
            Console.WriteLine("Customer {0} has following amounts: ", 
                cust.custname);

            foreach (var amount in cust.custamount)
            {
                Console.WriteLine(amount.Amount);
            }
        }

图像显示输出。

enter image description here

答案 1 :(得分:0)

试试这个:

foreach (var customer in customers)
{
    var matchingBalances = balances.Where(x => x.CustomerNumber == customer.CustomerNumber);
    customer.Balances.AddRange(matchingBalances);
}

答案 2 :(得分:0)

var combined = from c in customers
               join b in balance on b.CustomerNumber equal c.CustomerNumber
               select new{
                   Amount = b.Amount,
                   Balance = c.Balance,
                   };