• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
一天到晚游泳的鱼
博客园    首页    新随笔    联系   管理    订阅  订阅

LINQ体验(三)实现LINQ通用分页绑定方法

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

引用

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



  list的类型只有在运行时才能得到,怎么办呢!其实很简单我,我们可以使用 “参数推导泛型类型”的方法来实现: 

  看下面的代码(因为重点不在这里所以 代码写的比较粗糙): 

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

调用 

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

         

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

C#代码  
1.        public void BindBoundControl<TSource>(IEnumerable<TSource> DataSource, TSource type, GridView BoundControl, int PageSize)   
2.        {   
3.            this.BindBoundControl(DataSource, BoundControl, PageSize);   
4.        }  
        
public void BindBoundControl<TSource>(IEnumerable<TSource> DataSource, TSource type, GridView BoundControl, int PageSize)
        {
            
this.BindBoundControl(DataSource, BoundControl, PageSize);
        }




  调用 

C#代码  
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);     
7.        }  
posted @ 2011-03-04 18:00  Pato'  阅读(5361)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3