Get IEnumerable by page count - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IEnumerable

Description

Get IEnumerable by page count

Demo Code


using System.Collections;
using System;//from  w  ww  .j a v a 2  s .  c  o  m

public class Main{
        public static IEnumerable GetPage(this IEnumerable list, int pageNumber, int pageSize)
      {
         if (pageNumber <= 0 || pageSize <= 0)
         {
            throw new ArgumentException("pageNumber and pageSize must be greater than zero");
         }

         var startIndex = (pageNumber - 1) * pageSize;
         var endIndex = startIndex + pageSize - 1;

         var index = 0;
         foreach (var item in list)
         {
            if (startIndex <= index && index <= endIndex)
            {
               yield return item;
            }
            if (index > endIndex)
            {
               break;
            }
            index++;
         }
      }
}

Related Tutorials