title: Use linq to reorder a list into repeating groups based on element properties
body: <p>I'd like to know if a certain kind of ordering and collection of items that I am trying to do is possible with LINQ.</p>
  
  <p>I have a list of people all belonging to a certain subscription.</p>
  
  <p>A class for this might look like:</p>
  
  <pre><code>public class Person
  {
     public string name {get; set;}
     public int subscriptionId {get; set;}
  }
  </code></pre>
  
  <p>Let's say I have a list of data like:</p>
  
  <pre><code>Subscription ID | Name
  --------
  1 - bob
  1 - River
  5 - Phil
  2 - Jayne
  2 - Malcolm
  5 - Doug
  2 - Kaylee
  2 - Joe
  1 - Dan
  1 - Greg
  5 - Zoe
  2 - Carl
  5 - Kerney
  </code></pre>
  
  <p>and I'd want the list to end up like:</p>
  
  <pre><code>1 - bob
  1 - River
  2 - Jayne
  2 - Malcolm
  5 - Phil
  5 - Doug
  //  Note that the subscriber id repeats in the same pattern.
  1 - Dan
  1 - Greg
  2 - Kaylee
  2 - Joe
  5 - Zoe
  5 - Kerney
  // No more 1 or 3 subscibers so we have only Carl
  2 - Carl
  </code></pre>
  
  <p>In this example note that in the second list, we take 2 people from the first subscription, then two people from the second subscription, and so on. And then after going through all subscriptions, we start back over at the first subscription grabbing the next subscribers repeating the process until all subscribers have been enumerated.</p>
  
  <p><strong>EDIT:</strong>
  Basically I'm asking if it's possible in linq to take a single list of items and order them in a repeating pattern. So in the above example you would order by subscriptionId first, then you would take 2 move on to the next lowest subscriptionId and take 2 again, and keep in that order until you would need to repeat the process. so in the Example above it goes: 1,1,2,2,5,5,1,1,2,2,5,5,2</p>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>I wrote a quick little code snippet that I believe accomplishes what I'm trying to do. The reason for asking if a linq query existed was if its possible to get this functionality into one list and just loop through that list.</p>
  
  <pre><code>            List&lt;int&gt; subscriptionIds = people.Select(s =&gt; s.subscriptionId).Distinct().ToList();
  
              List&lt;List&lt;Person&gt;&gt; jaggedPersonArray = subscriptionIds.Select(sId =&gt; people.Where(s =&gt; s.subscriptionId == sId).ToList()).ToList();
  
              int subscriptionsHit = 0;
              int jaggedPersonArrayIndex = 0;
              int personsIndex = 0;
  
              while (people.Count - subscriptionsHit &gt; 0)
              {
                  List&lt;Person&gt; doStuffList = jaggedPersonArray[jaggedPersonArrayIndex].Skip(personsIndex).Take(2).ToList();
                  subscriptionsHit += doStuffList.Count;
                  //do stuff with doStuffList
                  if (jaggedPersonArrayIndex &gt;= jaggedPersonArray.Count - 1)
                  {
                      jaggedPersonArrayIndex = 0;
                      personsIndex += 2;
                  }
                  else
                  {
                      jaggedPersonArrayIndex++;
                  }
              }
  </code></pre>