none
Merging two lists RRS feed

  • Question

  • This must be a FAQ, but I can't find it.

     

    I have two lists:

     

              double[] xList = new double[] { 1, 2, 3};

              double[] yList = new double[] { 1, 4, 9};

     

    I now want to merge these into a single list with 3 elements { (1,1) , (2,4) , (3,9) }. How do I do this with LINQ? If I do

     

              var foo = from x in xList

                            from y in yList

                            select new { x , y };

     

    It appears to to a join and creates a list with 9 elements.

     

     

     

     

    Friday, October 1, 2010 10:05 AM

Answers

All replies

  • Hi,

    A crud way would be :

    from x in xList
    from y in yList
    where Array.IndexOf(xList, x) == Array.IndexOf(yList, y)
    select new { x, y }
    

    Regards,


    Syed Mehroz Alam
    My Blog | My Articles
    Friday, October 1, 2010 12:02 PM
  • That's very crude indeed. It also doesn't work if there are any duplicates.

    Wednesday, October 6, 2010 10:42 AM
  • That's very crude indeed. It also doesn't work if there are any duplicates.


    What do you want to happen in case of duplicates?

    Regards

    Wednesday, October 6, 2010 3:36 PM
  • Hi DanZeeZee,

    How about your problem ? Could you please explain more about the meaning of "any duplicates" and what do you want with these "duplicates"?  Thanks.

    Best regards,

    Alex Liang

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, October 14, 2010 9:45 AM
    Moderator
  • Basically, it should just return all pairs sequentially, i.e. when operating on

              double[] xList = new double[] { 1, 1, 3};

              double[] yList = new double[] { 1, 4, 9};

    it should return { { 1 , 1 } , { 1 , 4 } , { 3 , 9 } }.

    The above code returns { { 1 , 1 } , { 3 , 9 } }, methinks.

     

     

    Tuesday, November 9, 2010 3:09 PM
  • Basically, it should just return all pairs sequentially, i.e. when operating on

              double[] xList = new double[] { 1, 1, 3};

              double[] yList = new double[] { 1, 4, 9};

    it should return { { 1 , 1 } , { 1 , 4 } , { 3 , 9 } }.

    The above code returns { { 1 , 1 } , { 3 , 9 } }, methinks.


    Have you tried it?, I haven't test it properly but it should return the desired input, I'll check anyways

    Regards

    Tuesday, November 9, 2010 4:09 PM