1

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.

1
  • 4
    listA.Concat(listB).GroupBy(x => x.Period, x => Balance).Select(x => new { Key = x.Key, Balance = x.Sum() }).ToList() Commented Jan 22, 2015 at 11:21

4 Answers 4

5

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);
       }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Adding an example to your answer would make it a lot more useful to others.
2

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.

1 Comment

I disagree. Linq and its very recognizable methods are a lot more readable than any custom loops which are a little different with each developer. Linq lets you often code so that you describe what you want instead of how you want it to be done.
2

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.

Comments

0

// 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] + "";
    }

1 Comment

Thank you guys for your great inputs. It looks like i will have to go with for loop only . The main problem was that in my case the number of lists which are to be added is not fixed. So i was looking to reduce my for loops by using Linq but i also realize that it can complicate things.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.