Shift 'n Tab
Shift 'n Tab

Reputation: 9443

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?

Upvotes: 0

Views: 3405

Answers (2)

Mauro Sampietro
Mauro Sampietro

Reputation: 2814

You will need to know upfront how many records you have in total.

To do this simply use yourquery.Count(). It will translate to sql to some query beginning with SELECT COUNT(*)... and will not iterate over every single record.

Upvotes: 1

ViRuSTriNiTy
ViRuSTriNiTy

Reputation: 5155

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)));

Upvotes: 3

Related Questions