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

创新互联公司主要从事网页设计、PC网站建设(电脑版网站建设)、wap网站建设(手机版网站建设)、响应式网站开发、程序开发、网站优化、微网站、重庆小程序开发等,凭借多年来在互联网的打拼,我们在互联网网站建设行业积累了丰富的成都网站建设、网站设计、网站设计、网络营销经验,集策划、开发、设计、营销、管理等多方位专业化运作于一体。
#T#
在LINQ中,IQueryable 
- var list = from it in de.Customers where it.City == "abc" select new { it.City, it.Country };
 
list的类型只有在运行时才能得到,怎么办呢!其实很简单我,我们可以使用 “参数推导泛型类型”的方法来实现:
 看下面的代码(因为重点不在这里所以 代码写的比较粗糙):
- public void BindBoundControl
 (IEnumerable 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();
 - }
 
调用
- protected void Page_Load(object sender, EventArgs e)
 - {
 - NorthwindEntities de = new NorthwindEntities();
 - BindingUtils bind = new BindingUtils();
 - //先排序与一下再绑定
 - bind.BindBoundControl
 (de.Customers.OrderBy(v=>v.CustomerID), this.GridView1, 10); - }
 
下面我们只是需要重载一下我们的分页方法实现“参数推导泛型类型”就可以了 代码如下:
- public void BindBoundControl
 (IEnumerable DataSource, TSource type, GridView BoundControl, int PageSize) - {
 - this.BindBoundControl(DataSource, BoundControl, PageSize);
 - }
 
调用
- protected void Page_Load(object sender, EventArgs e)
 - {
 - NorthwindEntities de = new NorthwindEntities();
 - var list = from it in de.Customers where it.City == "abc" select new { it.City, it.Country };
 - BindingUtils bind = new BindingUtils();
 - bind.BindBoundControl(list.OrderBy(c=>c.City), list.FirstOrDefault(), this.GridView1, 10);
 - }
 
这个方法很简单的 只是通过 list.FirstOrDefault() 做参数 来推导 方法中 BindBoundControl
                本文标题:浅析LINQ通用分页绑定方法的实现
                
                URL链接:http://www.csdahua.cn/qtweb/news25/23825.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网