Note that there are some explanatory texts on larger screens.

plurals
  1. POMerging multiple lists
    primarykey
    data
    text
    <p>Given that there I have a list of lists, <code>List<code>&lt;List&lt;T&gt;&gt;</code></code>, in which all lists contain 0 or more items, likely, but not necessarily all the same number. And I would like tho have a list containing all the items in the Lists... but I want the order to be as follows: first the first items off all the lists, in order that they appear in the 'super-list'.</p> <p>Ex.</p> <pre><code>List[0] = { 'Apple', 'Blueberry', 'Cranberry' } List[1] = { 'Anteater', 'Baboon', 'Camel', 'Dodo'} List[2] = { 'Albatross', 'Blackbird', 'Chicken'} result = { 'Apple', 'Anteater', 'Albatross', 'Blueberry', 'Baboon', 'Blackbird', 'Cranberry', 'Camel', 'Chicken', 'Dodo' } </code></pre> <p>(note that this is NOT alphabetical order, Blueberry comes before Baboon)</p> <p>Ofcourse, i could just loop through the 'superlist' with a counter, adding items to the resultslist one by one, as long as there is a list that isn't empty:</p> <pre><code>int i = 0; bool done = false; while (!done) { bool found = false; foreach (list l in superlist) { if (l.Count() &gt; i) { found = true; result.Add(l[i]); } } i++; if (!found) done = true; } </code></pre> <p>But it would be way nicer to use some optimized LINQ function to do this. I have been looking into <a href="http://msdn.microsoft.com/en-us/library/dd267698.aspx" rel="nofollow">Zip</a>, <a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.groupby.aspx" rel="nofollow">GroupBy</a> and <a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.aggregate.aspx" rel="nofollow">Aggregate</a>, but couldn't get them to work.</p> <p>So: Is there a pretty LINQ function, or combination of multiple, that would turn this into pretty code, or should I stick by (and maybe optimize) my current function?</p> <p>Edit: A simple SelectMany(x => x) also doesn't do the trick, as it preserves the order of the lists, instead of folding them, like my algorithm does. See my initial question for more details.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload