locked
How do you combine two Lists into one on equal column value RRS feed

  • Question

  • User-1072848465 posted

    I have two lists

    List<FirstList> firstItems = new List<FirstList>();
    List<SecondList> secondItems = new List<SecondList>();

    FirstList contains
    id = 39, qty = 2
    id = 33, qty = 2
    id = 36, qty = 1
    id = 30, qty = 1

    SecondList contains
    id = 40, qty = 2
    id = 36, qty = 3
    id = 39, qty = 3

    When combining the lists if the id is the same the qty in the lists need to be added together. The result would look like this

    ThirdList
    id = 39, qty = 5
    id = 33, qty = 2
    id = 36, qty = 4
    id = 30, qty = 3
    id = 40, qty = 2

    Thanks for assistance
    Brad

    Friday, July 17, 2015 9:28 AM

Answers

  • User2103319870 posted

    bdassow

    When combining the lists if the id is the same the qty in the lists need to be added together. T

    You can use the Linq to combine list and sum up the values

    List<FirstList> firstItems = new List<FirstList>();
                List<FirstList> secondItems = new List<FirstList>();
    
                FirstList objfirslist = new FirstList();
                objfirslist.id = 39;
                objfirslist.qty = 2;
                firstItems.Add(objfirslist);
                FirstList objfirslist1 = new FirstList();
                objfirslist1.id = 33;
                objfirslist1.qty = 2;
                firstItems.Add(objfirslist1);
                FirstList objfirslist2 = new FirstList();
                objfirslist2.id = 36;
                objfirslist2.qty = 1;
                firstItems.Add(objfirslist2);
                FirstList objfirslist3 = new FirstList();
                objfirslist3.id = 30;
                objfirslist3.qty = 1;
                firstItems.Add(objfirslist3);
    
                FirstList objfirslist4 = new FirstList();
                objfirslist4.id = 40;
                objfirslist4.qty = 2;
                secondItems.Add(objfirslist4);
                FirstList objfirslist5 = new FirstList();
                objfirslist5.id = 36;
                objfirslist5.qty = 3;
                secondItems.Add(objfirslist5);
                FirstList objfirslist6 = new FirstList();
                objfirslist6.id = 39;
                objfirslist6.qty = 3;
                secondItems.Add(objfirslist6);
    		
    //Code to combine list and values var list = firstItems.AsEnumerable().Concat(secondItems.AsEnumerable()) .GroupBy(r => new { r.id}) .Select(g => new FirstList { id = g.Key.id, qty = g.Any(p => p.id == g.Key.id) ? g.Sum(k=> k.qty) : 0 });

    Ensure that you have added the below name space prior to using above code

    using System.Linq;

    Class

    class FirstList
        {
    
            public Int32 id {get;set;}   
            public Int32 qty {get;set;}  
        }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 17, 2015 10:35 AM

All replies

  • User2103319870 posted

    bdassow

    When combining the lists if the id is the same the qty in the lists need to be added together. T

    You can use the Linq to combine list and sum up the values

    List<FirstList> firstItems = new List<FirstList>();
                List<FirstList> secondItems = new List<FirstList>();
    
                FirstList objfirslist = new FirstList();
                objfirslist.id = 39;
                objfirslist.qty = 2;
                firstItems.Add(objfirslist);
                FirstList objfirslist1 = new FirstList();
                objfirslist1.id = 33;
                objfirslist1.qty = 2;
                firstItems.Add(objfirslist1);
                FirstList objfirslist2 = new FirstList();
                objfirslist2.id = 36;
                objfirslist2.qty = 1;
                firstItems.Add(objfirslist2);
                FirstList objfirslist3 = new FirstList();
                objfirslist3.id = 30;
                objfirslist3.qty = 1;
                firstItems.Add(objfirslist3);
    
                FirstList objfirslist4 = new FirstList();
                objfirslist4.id = 40;
                objfirslist4.qty = 2;
                secondItems.Add(objfirslist4);
                FirstList objfirslist5 = new FirstList();
                objfirslist5.id = 36;
                objfirslist5.qty = 3;
                secondItems.Add(objfirslist5);
                FirstList objfirslist6 = new FirstList();
                objfirslist6.id = 39;
                objfirslist6.qty = 3;
                secondItems.Add(objfirslist6);
    		
    //Code to combine list and values var list = firstItems.AsEnumerable().Concat(secondItems.AsEnumerable()) .GroupBy(r => new { r.id}) .Select(g => new FirstList { id = g.Key.id, qty = g.Any(p => p.id == g.Key.id) ? g.Sum(k=> k.qty) : 0 });

    Ensure that you have added the below name space prior to using above code

    using System.Linq;

    Class

    class FirstList
        {
    
            public Int32 id {get;set;}   
            public Int32 qty {get;set;}  
        }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 17, 2015 10:35 AM
  • User-1072848465 posted

    Thanks, worked great!

    Friday, July 17, 2015 3:17 PM