Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>How about following way of generating combinations using .Join method?</p> <pre><code>static void Main() { List&lt;List&lt;int&gt;&gt; collectionOfSeries = new List&lt;List&lt;int&gt;&gt; { new List&lt;int&gt;(){1, 2, 3, 4, 5}, new List&lt;int&gt;(){0, 1}, new List&lt;int&gt;(){6,3}, new List&lt;int&gt;(){1,3,5} }; int[] result = new int[collectionOfSeries.Count]; List&lt;List&lt;int&gt;&gt; combinations = GenerateCombinations(collectionOfSeries); Display(combinations); } </code></pre> <p>This Method <strong>GenerateCombinations(..)</strong> does main work of generating combinations. <strong>This method is generic so could be used for generating combinations of any type.</strong> </p> <pre><code>private static List&lt;List&lt;T&gt;&gt; GenerateCombinations&lt;T&gt;( List&lt;List&lt;T&gt;&gt; collectionOfSeries) { List&lt;List&lt;T&gt;&gt; generatedCombinations = collectionOfSeries.Take(1) .FirstOrDefault() .Select(i =&gt; (new T[]{i}).ToList()) .ToList(); foreach (List&lt;T&gt; series in collectionOfSeries.Skip(1)) { generatedCombinations = generatedCombinations .Join(series as List&lt;T&gt;, combination =&gt; true, i =&gt; true, (combination, i) =&gt; { List&lt;T&gt; nextLevelCombination = new List&lt;T&gt;(combination); nextLevelCombination.Add(i); return nextLevelCombination; }).ToList(); } return generatedCombinations; } </code></pre> <p>Display helper..</p> <pre><code>private static void Display&lt;T&gt;(List&lt;List&lt;T&gt;&gt; generatedCombinations) { int index = 0; foreach (var generatedCombination in generatedCombinations) { Console.Write("{0}\t:", ++index); foreach (var i in generatedCombination) { Console.Write("{0,3}", i); } Console.WriteLine(); } Console.ReadKey(); } </code></pre>
 

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