Paged IEnumerable - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IEnumerable

Description

Paged IEnumerable

Demo Code


using System.Linq;
using System.Collections.Generic;

public class Main{
        public static IEnumerable<T[]> Paged<T>(this IEnumerable<T> list, int pageSize)
        {//w  ww. ja  v a  2 s.  c o  m
            var result = new List<T[]>();

            var page = new List<T>();

            foreach (var item in list)
            {
                page.Add(item);
                if (page.Count == pageSize)
                {
                    result.Add(page.ToArray());
                    page = new List<T>();
                }
            }

            if (page.Any())
            {
                result.Add(page.ToArray());
            }

            return result;
        }
}

Related Tutorials