Click here to Skip to main content
15,896,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a collection with List type how can i apply paging on List<staff> collection
C#
public class Staff
{
some properties here...
}


Suppose if i get 100 records then i want make paging of 10 pages with page size 10
every time i click on link 1,2,3,4,,.....
I have to get that page with records of 10.

Can anyone help me to solve above problem


Thanks in Advance
Syed
Posted
Updated 20-Feb-14 20:33pm
v2

1 solution

You can easily do that with LINQ, like so:

C#
static IList<int> GetPage(IList<int> list, int pageNumber, int pageSize = 10)
{
    return list.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList();
}

static void Main()
{
	IList<int> list = Enumerable.Range(1, 100).ToList();
	
	Console.WriteLine(String.Join(", ", GetPage(list, 1, 10)));
	Console.WriteLine(String.Join(", ", GetPage(list, 2, 10)));
}


However, this does require you to have the whole collection in memory. If that's no problem, this is the way to go.

-edit-
*grumbles something about pasting on CP*
 
Share this answer
 
v2
Comments
[no name] 21-Feb-14 5:32am    
thanks a lot.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900