title: Loop with Paging and Limit C#
body: <p>How to stop a loop after 12 Items? </p>
  
  <pre><code>public class Program
  {
      private static void Main(string[] args)
      {
          var mapper = new Mapper();
          foreach (List&lt;int&gt; page in mapper.LoadListPaged(5, 12))
              Console.WriteLine($"Start: {page.Min()} --- End: {page.Max()}");
  
          Console.ReadLine();
       }
  }
  
  public class Mapper
  {
      private int _index = 0;
  
      public IEnumerable&lt;IEnumerable&lt;int&gt;&gt; LoadListPaged(int pageSize, int limit)
      {
          int pageCount = 0;
          var result = new List&lt;int&gt;();
  
          while (_index &lt; limit)
          {
              pageCount = 0;
              result = new List&lt;int&gt;();
  
              while (pageCount &lt; pageSize)
              {
                  pageCount++;
                  result.Add(_index++);
              }
              yield return result;
          }
      }
  }
  </code></pre>
  
  <p>The result should look like this:</p>
  
  <blockquote>
    <p>Start: 0 --- End: 4
    Start: 5 --- End: 9
    Start: 10 --- End: 12</p>
  </blockquote>
  
  <p>(Note it ends at 14, not 2)</p>
  
  <p>Thank you!</p>