如何在c#中对List集合应用分页 [英] How to apply paging on List collection in c#

查看:823
本文介绍了如何在c#中对List集合应用分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个List类型的集合如何在List< staff>上应用分页?收集

Hi,

I have a collection with List type how can i apply paging on List<staff> collection

public class Staff
{
some properties here...
}





假如我得到100条记录然后我想要每次点击链接1,2,3,4,.....每次点击10页,页面大小为10

我必须得到记录为10的页面。



任何人都可以帮我解决上述问题





提前致谢

Syed



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

推荐答案

您可以使用LINQ轻松完成此操作,如下所示:



You can easily do that with LINQ, like so:

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





但是,这确实要求你将整个集合放在内存中。如果这没问题,这就是要走的路。



-edit-

*抱怨粘贴的东西在CP *



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*


这篇关于如何在c#中对List集合应用分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆