【发布时间】:2015-03-21 03:56:39
【问题描述】:
所以这是我面临的一个问题 - 我有两个具有以下结构的列表
public class Payment
{
public int Period { get; set; }
public decimal Balance{ get; set; }
}
我创建了如下两个列表
List<Payment> A = new List<Payment>();
List<Payment> B = new List<Payment>();
列表如下所示。
List A List B
Perid Payment Perid Payment
1 10 1 16
2 12 2 13
3 45 3 44
4 23 4 33
5 36 5 34
6 45 6 35
我正在尝试从列表 A、B 中添加这两个付款并创建应该具有相同结构的第三个列表。
List C
Perid Payment
1 10+16
2 12+13
3 45+44
4 23+33
5 36+34
6 45+35
我理解 for 循环它是可能的,但无论如何,Linq OR Lambda 表达式可以以更简单的方式使用吗? 任何帮助都深表感谢。
【问题讨论】:
-
listA.Concat(listB).GroupBy(x => x.Period, x => Balance).Select(x => new { Key = x.Key, Balance = x.Sum() }).ToList()