昨天看了一会linq的书.今天哪来练练手.写了个简单的linq分页.在这里拿出来与大家共享一下.适合小数据型分页.因为他是先读取完全数据,然后在进行分页.
好了直接贴出代码
首页新建一个aspx页面
然后在后台aspx.cs文件中
1 protected void Page_Load(object sender, EventArgs e)
2 {
3 if (!IsPostBack)
4 {
5 int CurPage = 1;
6 int PageSize = 5;
7 int.TryParse(Request["CurPage"], out CurPage);
8 if (CurPage == 0)
9 CurPage = 1;
10 //获取总数据集合
11 IList<ContentsModel> objs = new List<ContentsModel>();
12 objs = Contents.GetAllContents();
13 //绑定数据显示控件
14 GridView1.DataSource = QueryByPage(PageSize, CurPage, objs);
15 GridView1.DataBind();
16 //获取分页目录
17 this.Label1.Text = GetQueryPagesMenu(PageSize, objs);
18 }
19 }
20 /// <summary>
21 /// 输入分页字符页码
22 /// </summary>
23 /// <param name="PageSize"></param>
24 /// <param name="objs"></param>
25 /// <returns></returns>
26 protected string GetQueryPagesMenu(int PageSize, IList<ContentsModel> objs)
27 {
28 int Count;
29 var db = objs;
30 var query = from cms_contents in db select cms_contents;
31 Count = (query.Count() / PageSize + 1);//不足一页按一页算
32 string PageMenu = "";
33 //拼接分页目录
34 for (int i = 1; i <= Count; i++)
35 {
36 PageMenu += "<a href='?CurPage=" + i.ToString() + "'>" + i.ToString() + "</a> | ";
37 }
38 return PageMenu;
39 }
40 /// <summary>
41 /// 获取分页后的数据集
42 /// </summary>
43 /// <param name="PageSize">每页显示的记录数</param>
44 /// <param name="CurPage">页数</param>
45 /// <param name="objs">数据总集合</param>
46 /// <returns></returns>
47 protected List<ContentsModel> QueryByPage(int PageSize, int CurPage, IList<ContentsModel> objs)
48 {
49 var query = from cms_contents in objs select cms_contents;
50 return query.Take(PageSize * CurPage).Skip(PageSize * (CurPage - 1)).ToList();
2 {
3 if (!IsPostBack)
4 {
5 int CurPage = 1;
6 int PageSize = 5;
7 int.TryParse(Request["CurPage"], out CurPage);
8 if (CurPage == 0)
9 CurPage = 1;
10 //获取总数据集合
11 IList<ContentsModel> objs = new List<ContentsModel>();
12 objs = Contents.GetAllContents();
13 //绑定数据显示控件
14 GridView1.DataSource = QueryByPage(PageSize, CurPage, objs);
15 GridView1.DataBind();
16 //获取分页目录
17 this.Label1.Text = GetQueryPagesMenu(PageSize, objs);
18 }
19 }
20 /// <summary>
21 /// 输入分页字符页码
22 /// </summary>
23 /// <param name="PageSize"></param>
24 /// <param name="objs"></param>
25 /// <returns></returns>
26 protected string GetQueryPagesMenu(int PageSize, IList<ContentsModel> objs)
27 {
28 int Count;
29 var db = objs;
30 var query = from cms_contents in db select cms_contents;
31 Count = (query.Count() / PageSize + 1);//不足一页按一页算
32 string PageMenu = "";
33 //拼接分页目录
34 for (int i = 1; i <= Count; i++)
35 {
36 PageMenu += "<a href='?CurPage=" + i.ToString() + "'>" + i.ToString() + "</a> | ";
37 }
38 return PageMenu;
39 }
40 /// <summary>
41 /// 获取分页后的数据集
42 /// </summary>
43 /// <param name="PageSize">每页显示的记录数</param>
44 /// <param name="CurPage">页数</param>
45 /// <param name="objs">数据总集合</param>
46 /// <returns></returns>
47 protected List<ContentsModel> QueryByPage(int PageSize, int CurPage, IList<ContentsModel> objs)
48 {
49 var query = from cms_contents in objs select cms_contents;
50 return query.Take(PageSize * CurPage).Skip(PageSize * (CurPage - 1)).ToList();
51 }
样式在这里就不多写了.