title: Combination Generator in Linq
body: <p>Is it possible to create some Linq that generates a List containing all possible combinations of a series of numbers??</p>
  
  <p>If you enter "21" it would generate a list with the elements:</p>
  
  <pre><code>list[0] = "21"
  list[1] = "22"
  list[2] = "11"
  list[3] = "12"
  </code></pre>
  
  <p>(Not nessesarily in that order)</p>
  
  <p>I understand you can use range to do things like:</p>
  
  <pre><code>List&lt;char&gt; letterRange = Enumerable.Range('a', 'z' - 'a' + 1).Select(i =&gt; (Char)i).ToList(); //97 - 122 + 1 = 26 letters/iterations
  </code></pre>
  
  <p>Which generates the alphabet from a-z. But I can not seem to transfer this knowledge to make a combination generator</p>
  
  <p>I have been able to figure it out with the following code, but it seems way too bulky and I am sure it can be done with a few lines. It really does feel like a bad solution I have made.</p>
  
  <p>Imagine I have called <code>GetAllCombinations("4321")</code> if it helps</p>
  
  <pre><code>public static String[] GetAllCombinations(String s)
  {
      var combinations = new string[PossibleCombinations(s.Length)];
  
      int n = PossibleCombinations(s.Length - 1);
  
      for (int i = 0; i &lt; s.Length; i++)
      {
          String sub;
          String[] subs;
  
          if (i == 0)
          {
              sub = s.Substring(1); //Get the first number
          }
          else if (i == s.Length - 1)
          {
              sub = s.Substring(0, s.Length - 1);
          }
          else
          {
              sub = s.Substring(0, i) + s.Substring(i + 1); 
          }
  
          subs = GetAllCombinations(sub);
  
          for (int j = 0; j &lt; subs.Length; j++)
          {
              combinations[i * n + j] = s[i] + subs[j];
          }
      }
  
      return combinations;
  }
  public static int PossibleCombinations(int n) //Combination possibilities. e.g 1-2-3-4 have 24 different combinations
  {
      int result = 1;
  
      for (int i = 1; i &lt;= n; i++)
          result *= i;
  
      return result;
  }
  </code></pre>