Suzane
Suzane

Reputation: 203

Adding two lists having same structure C#

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.

Upvotes: 1

Views: 1773

Answers (4)

Rahul Singh
Rahul Singh

Reputation: 21795

You have two options as already suggested:-

Using Concat + GroupBy :-

List<Payment> result = A.Concat(B).GroupBy(x => x.Period)
                                    .Select(x => new Payment
                                    {
                                        Period = x.Key,
                                        Balance = x.Sum(z => z.Balance)
                                    }).ToList();

Using Zip :-

List<Payment> result1 = A.Zip(B, (first, second) => new Payment
                                              {
                                                 Period = first.Period,
                                                 Balance = first.Balance + second.Balance
                                              }).ToList();

You can refer to this Fiddle.

Upvotes: 2

Ayman
Ayman

Reputation: 1

// Try for loop i think it would be good way to handle this situation there is other LINQ queries but i believe this is easier..

  List<int> a = new List<int>();
    a.Add(1 ) ;
    a.Add(2);
    List<int> b = new List<int>();
    b.Add(5) ;
    b.Add(6);
    List<int> c = new List<int>();
    for (int x = 0; x < a.Count; x++)
    {
        c.Add(a[x] + b[x]);
        Label1.Text += c[x] + "";
    }

Upvotes: 0

zmbq
zmbq

Reputation: 39013

I think you shouldn't do it. Write the code in the old-fashioned way is going to be cleared to almost anybody reading the code.

More importantly, the non-LINQ code will allow you to add sanity checks in a reasonable fashion (for example, are you sure all periods in the first list exist in the second? And vice versa?).

If you want to get more modern, I suggest using a generator, something like this:

IEnumerable<Payment> UnitePayments(List<Payment> list1, List<Payment> list2)
{
    ... Check that list1 and list2 are the same length ...
    for(int i=0; i<list1.Length; i++)
    {
         if(list1.Period!=list2.Period) ... handle this case...
         yield return new Payment { Period = list1.Period, 
                                    Balance = list1.Balance + list2.Balance };
    }
}

Your code readers will thank you.

Upvotes: 2

JunaidKirkire
JunaidKirkire

Reputation: 918

Try LINQ's Zip method. It helps you to iterate over two collections simultaneously.

Here's an example -

using System;
using System.Linq;

class Program
{
    static void Main()
    {
       // Two source arrays.
       var array1 = new int[] { 1, 2, 3, 4, 5 };
       var array2 = new int[] { 6, 7, 8, 9, 10 };

       // Add elements at each position together.
       var zip = array1.Zip(array2, (a, b) => (a + b));

       // Look at results.
       foreach (var value in zip)
       {
           Console.WriteLine(value);
       }
    }
}

Upvotes: 5

Related Questions