C# List 分页查询怎么实现

2024-11-14 05:04:07
推荐回答(5个)
回答1:

无语了,这个也能问的出来啊。。。。。下面的是java的代码,几乎是复制过去就能用。。。
/**
* 分页方法
*@returnType:List
* @param list 源数据
* @param currentPage 当前页
* @param maxNum 每页显示几条
* @param pageNum 总页数
* @return
*/
public static List getPageList(List list,int currentPage,int maxNum,int pageNum){

int fromIndex = 0; //从哪里开始截取
int toIndex = 0; //截取几个
if(list == null || list.size() == 0)
return null;
//当前页小于或等于总页数时执行
if( currentPage <= pageNum && currentPage != 0){
fromIndex = (currentPage - 1)*maxNum;

if(currentPage == pageNum){
toIndex = list.size();
}
else {
toIndex = currentPage*maxNum;
}

}
return list.subList(fromIndex, toIndex);
}

回答2:

使用linq:
//页数
int PageNum=2;

//每一页多少条数据
int Record=5;

//pageList是某一页的数据
var pageList=list.Skip(Record * (PageNum - 1)).Take(Record);

跟是bs和cs是无关的

回答3:

用一个全局变量记录list下标可以实现吧

回答4:

代码如下:

1 public partial class XTeacherFinder
2 {
3 private string m_TeacherId = string.Empty;
4
5 public string TeacherId
6 {
7 get { return m_TeacherId; }
8 set { this.m_TeacherId = value; }
9 }
10
11 private string m_TeacherName = string.Empty;
12
13 public string TeacherName
14 {
15 get { return this.m_TeacherName; }
16 set { this.m_TeacherName = value; }
17 }
18
19 public XTeacherFinder()
20 {
21
22 }
23
24 ///


25 /// 通过ID查找
26 ///

27 ///
28 ///
29 public bool FindTeacherById(XTeacher teacher)
30 {
31 return m_TeacherId == teacher.Id;
32 }
33
34 ///
35 /// 通过名称查找
36 ///

37 ///
38 ///
39 public bool FindTeacherByName(XTeacher teacher)
40 {
41 return m_TeacherName == teacher.Name;
42 }
43
44 ///
45 /// 通过ID和名称查找
46 ///

47 ///
48 ///
49 public bool FindTeacherByIdAndName(XTeacher teacher)
50 {
51 return (m_TeacherName == teacher.Name && m_TeacherId == teacher.Id);
52 }
53 }

回答5:

int pageNum=1; //当前页
int ListPageIndex=0; //List中的下标

ListPageIndex=(pageNum-1)*5; //当前页第一条的下标
for(int i=0;i<5;i++)
{
Console.WriteLine(list[ListPageIndex+i].toString());
}

PS:你每次翻页,需要传入 PageSize
下一页就是 +1
上一页就是 -1