浅析LINQ通用分页绑定方法的实现

开发 后端
本文将简单讨论一下实现LINQ通用分页绑定方法,希望通过本文能对大家用好LINQ有所帮助。

在这里我们将讨论LINQ通用分页绑定方法,希望通过本文能对大家了解LINQ通用分页有所帮助,在这里将展示更多的代码。

#T#

在LINQ中,IQueryable <T>接口和IEnumerable <T>接口都分别提供了Skip方法和Take方法,用来做分页非常合适.因此我就想用他们做一个分页控件,因为IQueryable <T> 是继承自 IEnumerable <T> 的。因此使用接口仅需要针对后者就可以了。使用的时候只需提供数据源、绑定的GridView的、每页大小即可。现在问题就出了在数据源上,要求用户提供一个数据源类型,即IQueryable <T>接口和IEnumerable <T>接口? T是可确定类型(已知类型)的话还可以,若T是匿名类型,如

  1. var list = from it in de.Customers where it.City == "abc" select new { it.City, it.Country };  

list的类型只有在运行时才能得到,怎么办呢!其实很简单我,我们可以使用 “参数推导泛型类型”的方法来实现:
看下面的代码(因为重点不在这里所以 代码写的比较粗糙):

  1. public void BindBoundControl<TSource>(IEnumerable<TSource> DataSource, GridView BoundControl, int PageSize)  
  2.         {  
  3.  
  4.             //获取总记录数(这里可以使用参数传入总页数 就不必每次都执行下面方法)  
  5.             int totalRecordCount = DataSource.Count();  
  6.  
  7.             //计算总页数  
  8.             int totalPageCount = 0;  
  9.  
  10.             if (PageSize == 0)  
  11.             {  
  12.                 PageSize = totalRecordCount;  
  13.             }  
  14.             if (totalRecordCount % PageSize == 0)  
  15.             {  
  16.                 totalPageCount = totalRecordCount / PageSize;  
  17.             }  
  18.             else 
  19.             {  
  20.                 totalPageCount = totalRecordCount / PageSize + 1;  
  21.             }  
  22.  
  23.             //从参数中获取当前页码  
  24.             int CurrentPageIndex = 1;  
  25.  
  26.             //如果从参数中获取页码不正确 设置页码为第一页  
  27. if (!int.TryParse(HttpContext.Current.Request.QueryString["Page"], out CurrentPageIndex) || CurrentPageIndex <= 0 || CurrentPageIndex > totalPageCount)  
  28.             {  
  29.                 CurrentPageIndex = 1;  
  30.             }  
  31.             //绑定数据源  
  32.             BoundControl.DataSource = DataSource.Skip((CurrentPageIndex - 1) * PageSize).Take(PageSize);  
  33.             BoundControl.DataBind();  
  34.         } 

调用 

  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     NorthwindEntities de = new NorthwindEntities();  
  4.  
  5.     BindingUtils bind = new BindingUtils();  
  6.     //先排序与一下再绑定  
  7.     bind.BindBoundControl<Customers>(de.Customers.OrderBy(v=>v.CustomerID), this.GridView1, 10);    

下面我们只是需要重载一下我们的分页方法实现“参数推导泛型类型”就可以了 代码如下:

  1. public void BindBoundControl<TSource>(IEnumerable<TSource> DataSource, TSource type, GridView BoundControl, int PageSize)  
  2. {  
  3.     this.BindBoundControl(DataSource, BoundControl, PageSize);  

调用

  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     NorthwindEntities de = new NorthwindEntities();  
  4.     var list = from it in de.Customers where it.City == "abc" select new { it.City, it.Country };  
  5.     BindingUtils bind = new BindingUtils();  
  6.     bind.BindBoundControl(list.OrderBy(c=>c.City), list.FirstOrDefault(), this.GridView1, 10);    

这个方法很简单的 只是通过 list.FirstOrDefault() 做参数 来推导 方法中 BindBoundControl<TSource> 的TSource 就可以了,当然因为每次分页时都会执行 list.FirstOrDefault() 会损失一点点的效率。

原文标题:非常简单的实现LINQ通用分页绑定方法

链接:http://www.cnblogs.com/ejiyuan/archive/2009/12/22/1629806.html

责任编辑:彭凡 来源: 博客园
相关推荐

2009-09-17 08:47:00

Linq插入数据

2009-09-14 16:29:39

LINQ嵌套

2009-09-14 19:14:51

LINQ动态查询

2009-09-17 09:24:57

Linq实现分页

2009-09-14 09:46:00

LINQ to SQL

2009-09-14 18:23:59

LINQ嵌套查询

2009-09-14 19:55:03

LINQ事务处理

2009-09-13 21:52:16

LINQ字符串

2009-09-15 14:30:11

Linq连接

2009-09-14 19:20:22

LINQ TO SQL

2009-09-17 17:34:23

linq to sql

2012-09-18 09:39:57

Linq项目高效

2009-04-02 10:37:52

通用基类SQLLINQ

2009-09-17 13:30:32

LINQ to XML

2009-09-10 18:02:23

LINQ to SQL

2009-09-14 16:46:15

LINQ to XML

2009-09-16 17:11:35

LINQ To SQL

2009-09-16 15:33:22

LINQ to XML

2009-09-15 13:30:54

linq级联

2009-09-07 16:44:28

Linq String
点赞
收藏

51CTO技术栈公众号