goliatpiotr
goliatpiotr

Reputation: 93

Combine two jagged lists into one

Im having two jagged lists.

First one:

List<List<string>> firstList = new List<List<string>>();

{ dd ff }
{ dd ff }
{ dd ff }

Second one:

List<List<string>> secondList = new List<List<string>>();

{ gg hh }
{ gg uu }
{ hh uu }

Im having a problem with combining both lists into one like this one:

{ dd ff gg hh }
{ dd ff gg uu }
{ dd ff hh uu }

Any Concatenation attempt resulted in just adding secondList elements like another firstList rows like this:

{ dd ff }
{ dd ff }
{ dd ff }
{ gg hh }
{ gg uu }
{ hh uu }

Would appreciate any help with this one!

Upvotes: 1

Views: 302

Answers (4)

Ian
Ian

Reputation: 30813

The important idea here is you have to concat each element of the list rather than concatting the two lists.

  1. Concat each element:

    { dd ff gg hh } //each element dd ff is concatted with gg hh
    { dd ff gg uu }
    { dd ff hh uu }
    
  2. Concat the lists:

    { dd ff }
    { dd ff }
    { dd ff }   //elements from the 1st list
    ----------- concated with
    { gg hh }   
    { gg uu }
    { hh uu }  //elements from the 2nd list
    

You could Concat each element of the lists (which have to have same number of elements or the second one more number than the first) like this:

List<List<string>> finalList = (from x in firstList
                                join y in secondList
                                on firstList.IndexOf(x) equals secondList.IndexOf(y)
                                select x.Concat(y).ToList()).ToList();

Or any other way to feel like to do it.

Upvotes: 1

chief7
chief7

Reputation: 14383

Another option is just loop through the first list and add the elements from the second list. For example:

var list1 = new List<List<string>> {
    new List<string> { "dd", "ff" },
    new List<string> { "dd", "ff" },
    new List<string> { "dd", "ff" }};

var list2 = new List<List<string>> {
    new List<string> { "gg", "hh" },
    new List<string> { "gg", "uu" },
    new List<string> { "hh", "uu" }};

for(var j = 0; j < list1.Count(); j++)
{
    list1[j].AddRange(list2[j]);
}

Upvotes: 0

Dmitry S.
Dmitry S.

Reputation: 8503

You can use Zip extension method that is a part of the System.Linq namespace. Tuples can be used to associate values form both lists. Or you can just Concat inner lists.

var list1 = new List<List<string>>
{
    new List<string> { "dd", "ff" },
    new List<string> { "dd", "ff" },
    new List<string> { "dd", "ff" },
};

var list2 = new List<List<string>>
{
    new List<string> { "gg", "hh" },
    new List<string> { "gg", "uu" },
    new List<string> { "hh", "uu" },
};

var result = list1.Zip(list2, (l1, l2) => Tuple.Create(l1, l2)).ToList();

Upvotes: 2

juharr
juharr

Reputation: 32266

You can using Zip to do that to concatenate the corresponding sub-lists.

var combined = firstList.Zip(secondList, (f,s) => f.Concat(s).ToList()).ToList();

Note that if the two lists contain a different number of sub-lists the result will only have as many as the shorter of the two lists.

Upvotes: 3

Related Questions