How to

make paging in collections

Published: 11. November 2011 | Updated: 11. November 2011
License: Microsoft Public License (MS-PL)
Categories: Collections
Tags: C# Collections Linq
Was this snippet helpful for you? YESYES / NONO

Class for paging for generic collections

Import namespaces

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

Class for paging

public class PagingCollection<T> : IEnumerable<T>
{
    #region fields
    private const int DefaultPageSize = 10;
    private readonly IEnumerable<T> _collection;
    private int _pageSize = DefaultPageSize;
    #endregion

    #region properties
    /// <summary>
    /// Gets or sets page size
    /// </summary>
    public int PageSize
    {
        get
        {
            return _pageSize;
        }
        set
        {
            if (value <= 0)
            {
                throw new ArgumentException();
            }
            _pageSize = value;
        }
    }

    /// <summary>
    /// Gets pages count
    /// </summary>
    public int PagesCount
    {
        get
        {
            return (int)Math.Ceiling(_collection.Count() / (decimal)PageSize);
        }
    }
    #endregion

    #region ctor
    /// <summary>
    /// Creates paging collection and sets page size
    /// </summary>
    public PagingCollection(IEnumerable<T> collection, int pageSize)
    {
        if (collection == null)
        {
            throw new ArgumentNullException("collection");
        }
        PageSize = pageSize;
        _collection = collection.ToArray();
    }

    /// <summary>
    /// Creates paging collection
    /// </summary>
    public PagingCollection(IEnumerable<T> collection)
        : this(collection, DefaultPageSize)
    { }
    #endregion

    #region public methods
    /// <summary>
    /// Returns data by page number
    /// </summary>
    public IEnumerable<T> GetData(int pageNumber)
    {
        if (pageNumber < 0 || pageNumber > PagesCount)
        {
            return new T[] { };
        }

        int offset = (pageNumber - 1) * PageSize;

        return _collection.Skip(offset).Take(PageSize);
    }

    /// <summary>
    /// Returns number of items on page by number
    /// </summary>
    public int GetCount(int pageNumber)
    {
        return GetData(pageNumber).Count();
    }
    #endregion

    #region static methods
    /// <summary>
    /// Returns data by page number and page size
    /// </summary>
    public static IEnumerable<T> GetPaging(IEnumerable<T> collection, int pageNumber, int pageSize)
    {
        return new PagingCollection<T>(collection, pageSize).GetData(pageNumber);
    }

    /// <summary>
    /// Returns data by page number
    /// </summary>
    public static IEnumerable<T> GetPaging(IEnumerable<T> collection, int pageNumber)
    {
        return new PagingCollection<T>(collection, DefaultPageSize).GetData(pageNumber);
    }
    #endregion

    #region IEnumerable<T> Members
    /// <summary>
    /// Returns an enumerator that iterates through collection
    /// </summary>
    public IEnumerator<T> GetEnumerator()
    {
        return _collection.GetEnumerator();
    }
    #endregion

    #region IEnumerable Members
    /// <summary>
    /// Returns an enumerator that iterates through collection
    /// </summary>
    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
    #endregion
}

Use

// fill test data
var data = new List<int>();
for (int i = 0; i < 25; i++)
{
    data.Add(i);
}

// create paging collection
var paging = new PagingCollection<int>(data);
// set page size
paging.PageSize = 6;

// get number of pages
Console.WriteLine("Pages count: {0}", paging.PagesCount);

// iterate through pages
for (int i = 1; i <= paging.PagesCount; i++)
{
    // get number of items on page
    Console.WriteLine("Page: {0} ({1} items)", i, paging.GetCount(i));

    // get data by page number
    foreach (int number in paging.GetData(i))
    {
        Console.WriteLine(number);
    }
}
Console Output:
Pages count: 5
Page: 1 (6 items)
0
1
2
3
4
5
Page: 2 (6 items)
6
7
8
9
10
11
Page: 3 (6 items)
12
13
14
15
16
17
Page: 4 (6 items)
18
19
20
21
22
23
Page: 5 (1 items)
24
Send us feedback about this snippet »



Related Snippets: