So here a problem which i am facing - I have two lists with following structure
public class Payment
{
public int Period { get; set; }
public decimal Balance{ get; set; }
}
I have created following two lists as below
List<Payment> A = new List<Payment>();
List<Payment> B = new List<Payment>();
The list looks like this.
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
I am trying to add these two Payments from list A,B and create a third list which should have same structure.
List C
Perid Payment
1 10+16
2 12+13
3 45+44
4 23+33
5 36+34
6 45+35
I understand with for looping its possible but is there anyway where Linq OR Lambda expressions can be used in simpler way? Any help is deeply appreciated.
listA.Concat(listB).GroupBy(x => x.Period, x => Balance).Select(x => new { Key = x.Key, Balance = x.Sum() }).ToList()