Search code examples
c#linqpaginationienumerableenumeration

How to get the total page of LINQ pagination


Based on this answer on how to apply paging on a list.

I would like to extend this question of how can we get the total page of a list with a specific total collection of item per page?

For example, suppose i have 50 items each page has 12 items. How can i get the total page?


Solution

  • Isn't it simple math?

    Page count = Round(Total Records / Page Size)

    in C#

    int totalItemCount = myList.Count();
    int pageSize = 10;
    
    int pageCount = System.Convert.ToInt32(System.Math.Ceiling(totalItemCount / System.Convert.ToDouble(pageSize)));