title: Cartesian Product in C# using enumeratos
body: <p>I'm trying to implement one of the solutions found in the question <a href="https://stackoverflow.com/questions/710670/c-sharp-permutation-of-an-array-of-arraylists">C# Permutation of an array of arraylists?</a>
  It should perform the the cartesian product, but instead it returns the right number of lists, but each list is always the just the first of each array.  The code and results are below.  </p>
  
  <pre><code>using System;
  using System.Collections;
  using System.Collections.Generic;
  using System.Linq;
  
  namespace TestCartProd
  {
      class MainClass
      {
          public static void Main (string[] args)
          {
              string[][] myList = new string[3][];
              myList[0] = new string[] { "1", "5", "3", "9" };
              myList[1] = new string[] { "2", "3" };
              myList[2] = new string[] { "a", "93" };
  
              List&lt;IEnumerable&lt;string&gt;&gt; v = GetPermutations (myList).ToList();
  
              foreach (IEnumerable t in v) {
                  foreach (string u in t) {
                      Console.Write (u);
                  }
                  Console.WriteLine ();
              }
  
          }
  
          public static IEnumerable&lt;IEnumerable&lt;T&gt;&gt; GetPermutations&lt;T&gt;(IEnumerable&lt;IEnumerable&lt;T&gt;&gt; lists)
          {
              // Check against an empty list.
              if (!lists.Any())
              {
                  yield break;
              }
  
              // Create a list of iterators into each of the sub-lists.
              List&lt;IEnumerator&lt;T&gt;&gt; iterators = new List&lt;IEnumerator&lt;T&gt;&gt;();
              foreach (var list in lists)
              {
                  var it = list.GetEnumerator();
                  // Ensure empty sub-lists are excluded.
                  if (!it.MoveNext())
                  {
                      continue;
                  }
                  iterators.Add(it);
              }
  
              bool done = false;
              while (!done)
              {
                  // Return the current state of all the iterator, this permutation.
                  yield return from it in iterators select it.Current;
  
                  // Move to the next permutation.
                  bool recurse = false;
                  var mainIt = iterators.GetEnumerator();
                  mainIt.MoveNext(); // Move to the first, succeeds; the main list is not empty.
                  do
                  {
                      recurse = false;
                      var subIt = mainIt.Current;
                      if (!subIt.MoveNext())
                      {
                          subIt.Reset(); // Note the sub-list must be a reset-able IEnumerable!
                          subIt.MoveNext(); // Move to the first, succeeds; each sub-list is not empty.
  
                          if (!mainIt.MoveNext())
                          {
                              done = true;
                          }
                          else
                          {
                              recurse = true;
                          }
                      }
                  }
                  while (recurse);
              }
          }
      }
  }
  </code></pre>
  
  <p>Results:
  12a
  12a
  12a
  12a
  12a
  12a
  12a
  12a
  12a
  12a
  12a
  12a
  12a
  12a
  12a
  12a</p>