Click here to Skip to main content
12,827,807 members (48,089 online)
Click here to Skip to main content
Add your own
alternative version


9 bookmarked
Posted 16 Jul 2014

A Pagination Solution using IEnumerable<T>

, 17 Jul 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
An elegant approach to pagination


It's been a while since I last wrote an article. I like coding, but I'm not a very good writer. However, sometimes, I do feel the need to share my work to others. Maybe, just maybe, there's someone out there scratching his/her head off trying to solve the same problem I already solved. And recently, I had a eureka moment. I was so excited I tried sharing it to my girlfriend, but I got a blank stare in return. So, I'm sharing it to you!

Most of us had to deal with pagination. I noticed I've tackled this issue so many times before, but I never had a real solution. I never had a solution that made myself go "wow!" I always used a quick and dirty implementation just so I can get past it. Last week, that changed and today, I'm giving it to you!


You see, pagination is not as simple as it looks. True, you can throw in a few lines of code and voila! You've got yourself a working application. I've seen a lot of implementation myself, but nothing really satisfied me. Here's one example:

public IEnumerable<ComplexType> GetComplexTypes(int pageSize, int pageNumber)
    // TODO: Validate parameters here ...
    return someContext.Get<ComplexType>()
        .Skip(pageSize * (pageNumber - 1))

Okay, that looks good, you might say. It does give you the basic functionality of pagination. However, suppose I have a Grid View on my web page that should display the maximum page number in the footer section. How would I know what number to display?



Using the code

Before we dive right in to the details, let me show you how easy it is to use the code:

// Let's say you have a repository class like this
public class Repository<T> : IRepository<T>
    private readonly IContext someContext;

    public Repository(IContext someContext)
        this.someContext = someContext;

    // Pass in a Paging object as a parameter
    public IEnumerable<T> GetComplexTypes(Paging paging)
        // TODO: Validate parameters here ...
        return this.someContext.Get<T>()
            .Paginate<T>(paging); // Then call the .Paginate<T>() method

// This is just an example to show how to get the page count
public class SomeService
    private readonly IRepository<ComplexType> repository;

    public SomeService(IRepository<ComplexType> repository)
        this.repository = repository;

    public List<ComplexType> GetComplexTypes(int pageSize, int pageNumber, out int pageCount)
        var paging = new Paging(pageSize, pageNumber);
        var complexTypes = this.repository
        pageCount = paging.PageCount; // Page count is available after calling the .ToList() method
        return complexTypes;

That's pretty much all you need to know in terms of using the code, but it's probably better to understand how it works.


There are only 3 things you need to keep in mind to understand how it all works:

  1. The Paging class - a placeholder for all paging-related parameters such as the page size, page number, and the page count (like an out parameter).
  2. The PagedEnumerable class - implementation of System.Collections.Generic.IEnumerable<T>. The most important logic is in this class.
  3. The EnumerableExtension class - extends the System.Collections.Generic.IEnumerable<T> interface with a Paginate() method.

The Paging class

Apart from having paging-related properties mentioned above, this class also takes care of calculating the value of the PageCount property when the CalculateAndSetPageCount() method is executed. You don't need to call this method yourself. This is invoked automatically in the PagedEnumerable.DoGetEnumerator() method when an Enumerable extension method such as ToList() or ToArray() is called. All you have to do is to pass an instance of this class to the Paginate() method.

NOTE: As you may have noticed by now, I prefer using page number instead of page index. If you're more comfortable working with page index, feel free to modify the code to satisfy your needs.

The PagedEnumerable class

The PagedEnumerable class is an internal class and it is the most important component of the code. The magic happens in the DoGetEnumerator() method. This method keeps count of all the items in the collection. However, it returns only those items that satisfy the conditions set against the Paging.PageSize and Paging.PageNumber properties.

private IEnumerator<T> DoGetEnumerator()
    var count = 0;
    var size = 0;
    var skip = this.paging.GetSkip();
    var enumerator = this.collection.GetEnumerator();
    while (enumerator.MoveNext())
        if (++count <= skip)
        if (++size > this.paging.PageSize)
        yield return enumerator.Current;

As you can see, the Paging.CalculateAndSetPageCount() method is called right before the the end of the DoGetEnumerator() method is reached.

The Paginate method

This is the glue that holds all the components together. This is what communicates to the outside world. This is where the magic begins. This method simply returns a PagedEnumerable<T> object as an IEnumerable<T>. It's that simple. It's clean and it's elegant!


Thank you for reading my article! I hope you liked it. The best way for you to understand how the code works is to get your hands dirty. So, feel free to download and use it.

A word of caution: I have never used or tested this code in scenarios which involve big data sets. I don't expect it to work efficiently in such cases. So, use it with caution.

That' all folks. Happy coding!


  • 17 July 2014: Initial version
  • 17 July 2014: Fixed image link


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Marlon Hizole
Software Developer
Philippines Philippines
Marlon is a Filipino Software Engineer.
Marlon is living and working in Singapore.
Marlon enjoys coding in his spare time.
Marlon likes object oriented programming.
Marlon loves C#.

You may also be interested in...


Comments and Discussions

QuestionHow is the performance Pin
johannesnestler21-Jul-14 5:05
memberjohannesnestler21-Jul-14 5:05 
AnswerRe: How is the performance Pin
Marlon Hizole21-Jul-14 16:33
memberMarlon Hizole21-Jul-14 16:33 
QuestionLet the Database do the Work Pin
PeejayAdams17-Jul-14 4:01
memberPeejayAdams17-Jul-14 4:01 
AnswerRe: Let the Database do the Work Pin
John Brett17-Jul-14 5:49
memberJohn Brett17-Jul-14 5:49 
AnswerRe: Let the Database do the Work Pin
Marlon Hizole17-Jul-14 7:33
memberMarlon Hizole17-Jul-14 7:33 
QuestionMy vote 5 Pin
Nitin Sawant17-Jul-14 3:07
professionalNitin Sawant17-Jul-14 3:07 
AnswerRe: My vote 5 Pin
Marlon Hizole17-Jul-14 7:07
memberMarlon Hizole17-Jul-14 7:07 
BugImage broken link? Pin
Matt T Heffron16-Jul-14 10:36
professionalMatt T Heffron16-Jul-14 10:36 
GeneralRe: Image broken link? Pin
Marlon Hizole16-Jul-14 15:54
memberMarlon Hizole16-Jul-14 15:54 
GeneralRe: Image broken link? Pin
Marlon Hizole18-Jul-14 5:25
memberMarlon Hizole18-Jul-14 5:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170326.1 | Last Updated 17 Jul 2014
Article Copyright 2014 by Marlon Hizole
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid