title: how do I chunk an enumerable?
body: <p>I need an elegant method that takes an enumerable and gets the enumerable of enumerables each of the same number of elements in it but the last one:</p>
  
  <pre><code>public static IEnumerable&lt;IEnumerable&lt;TValue&gt;&gt; Chunk&lt;TValue&gt;(this IEnumerable&lt;TValue&gt; values, Int32 chunkSize)
  {
      // TODO: code that chunks
  }
  </code></pre>
  
  <p>This is what I have tried:</p>
  
  <pre><code>    public static IEnumerable&lt;IEnumerable&lt;TValue&gt;&gt; Chunk&lt;TValue&gt;(this IEnumerable&lt;TValue&gt; values, Int32 chunkSize)
      {
          var count = values.Count();
          var numberOfFullChunks = count / chunkSize;
          var lastChunkSize = count % chunkSize;
          for (var chunkIndex = 0; chunkSize &lt; numberOfFullChunks; chunkSize++)
          {
              yield return values.Skip(chunkSize * chunkIndex).Take(chunkSize);
          }
          if (lastChunkSize &gt; 0)
          {
              yield return values.Skip(chunkSize * count).Take(lastChunkSize);
          }
      }
  </code></pre>
  
  <p><strong>UPDATE</strong>
  Just discovered there was a similar topic about splitting a list <a href="https://stackoverflow.com/questions/419019/split-list-into-sublists-with-linq">Split List into Sublists with LINQ</a></p>