An Optimized Pagination Solution using generic class and IEnumerable<T>

Views: 1863
Comments: 0
Like/Unlike: 2
Posted On: 04-Oct-2017 09:09 

Share:   fb twitter linkedin
Kailash...
206 Points
23 Posts
Introduction
 
Most of us know the power of pagination when there is large data. Mostly we pass items-per-page and page-number as parameter to a server side method and returns items equal to items-per-page using skip/take mechanism. we always used a quick and dirty implementation. This article will help you to generate optimized pagination in a general solution.
 
Detail
 
Pagination class with static method GetPage:
 
public class Pagination
{
  public static PaginationViewModel<T> GetPage<T>(IEnumerable<T> list, int pageNumber, int itemsPerPage)
  {
    PaginationViewModel<T> res = new PaginationViewModel<T>() { ItemsPerPage = itemsPerPage };
    List<T> pageItemList = new List<T>();

    res.TotalItemsCount = list.Count();
    if (res.TotalItemsCount > 0)
    {
      res.PageCount = res.TotalItemsCount / res.ItemsPerPage + (res.TotalItemsCount % res.ItemsPerPage == 0 ? 0 : 1);
      res.PageIndex = pageNumber > res.PageCount - 1 ? res.PageCount - 1 : pageNumber;
      pageItemList.AddRange(list.Skip(res.PageIndex * res.ItemsPerPage).Take(res.ItemsPerPage));
    }
    else
    {
      res.PageCount = 0;
      res.PageIndex = 0;
    }
    res.Items = pageItemList;

    return res;
  }
}​
 
public class PaginationViewModel<T>
{
  public int PageCount { get; set; }
  public int TotalItemsCount { get; set; }
  public int PageIndex { get; set; }
  public int ItemsPerPage { get; set; }
  public IEnumerable<T> Items { get; set; }
}​
 
 
 
Example
 
Now, let's implement above generic method class for pagination. We will pass simple (with less/required columns) list object in GetPage method i.e. id, any other fields that will be use for sorting/ordering items.
 
Suppose we need to show pagination on list of students order by name from three tables Student, Address and Course as:
  1. Student => StudentId, Name
  2. Address => AddressId, StudentId, MobileNo, etc.
  3. Course  => CourseId, StudentId, CourseName, Desc, etc.
 
And we need to show student detail on page as:
StudentId, Name, MobileNo, CourseName
 
Lets create a view model class for it as:
 
Public class StudentViewModel
{
  Public long StudentId {get; set;}
  Public string Name {get; set;}
  Public string MobileNo {get; set;}
  Public string CourseName {get; set;}
}
 
Now, we will get per-page item from following method:
 
 
public PaginationViewModel<StudentViewModel> GetStudent(int page, int itemsPerPage)
{
  var students =//get students from table Student
  var studentPage = Pagination.GetPage(students, page, itemsPerPage);
  foreach (var student in studentPage.Items)
  {
// Use other two tables Address and Course to set other properties for only items per page
  }
  return studentPage
}
 
Call this method as follow:
 
//First page 
GetStudent(0, 20);

//Secong page
GetStudent(1, 20);
//---
//and so on....
 
Conclusion
In this article we learnt optimized way for pagination solution. Hope it will be helpful.
 
0 Comments
 Log In to Chat