Combine two jagged lists into one

淺唱寂寞╮ 提交于 2019-12-23 02:29:14

问题


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!


回答1:


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();



回答2:


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.




回答3:


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.




回答4:


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]);
}


来源:https://stackoverflow.com/questions/35076239/combine-two-jagged-lists-into-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!