title: Generating Permutations using LINQ
body: <p>I have a set of products that must be scheduled. There are P products each indexed from 1 to P. Each product can be scheduled into a time period 0 to T. I need to construct all permutations of product schedules that satisfy the following constraint:</p>
  
  <pre><code>If p1.Index &gt; p2.Index then p1.Schedule &gt;= p2.Schedule.
  </code></pre>
  
  <p>I am struggling to construct the iterator. I know how to do this via LINQ when the number of products is a known constant, but am not sure how to generate this query when the number of products is an input parameter.</p>
  
  <p>Ideally I would like to use the yield syntax to construct this iterator.</p>
  
  <pre><code>public class PotentialSchedule()
  {
        public PotentialSchedule(int[] schedulePermutation)
        {
               _schedulePermutation = schedulePermutation;
        }
        private readonly int[] _schedulePermutation;
  }
  
  
  private int _numberProducts = ...;
  public IEnumerator&lt;PotentialSchedule&gt; GetEnumerator()
  {
       int[] permutation = new int[_numberProducts];
       //Generate all permutation combinations here -- how?
       yield return new PotentialSchedule(permutation);
  }
  </code></pre>
  
  <p>EDIT: Example when _numberProducts = 2</p>
  
  <pre><code>public IEnumerable&lt;PotentialSchedule&gt; GetEnumerator()
  {
      var query = from p1 in Enumerable.Range(0,T)
                  from p2 in Enumerable.Range(p2,T)
                  select new { P1 = p1, P2 = p2};
  
      foreach (var result in query)
            yield return new PotentialSchedule(new int[] { result.P1, result.P2 });
  }
  </code></pre>