日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
淺析LINQ通用分頁綁定方法的實(shí)現(xiàn)

?在這里我們將討論LINQ通用分頁綁定方法,希望通過本文能對大家了解LINQ通用分頁有所幫助,在這里將展示更多的代碼。

創(chuàng)新互聯(lián)公司主要從事網(wǎng)頁設(shè)計(jì)、PC網(wǎng)站建設(shè)(電腦版網(wǎng)站建設(shè))、wap網(wǎng)站建設(shè)(手機(jī)版網(wǎng)站建設(shè))、響應(yīng)式網(wǎng)站開發(fā)、程序開發(fā)、網(wǎng)站優(yōu)化、微網(wǎng)站、重慶小程序開發(fā)等,憑借多年來在互聯(lián)網(wǎng)的打拼,我們在互聯(lián)網(wǎng)網(wǎng)站建設(shè)行業(yè)積累了豐富的成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、網(wǎng)站設(shè)計(jì)、網(wǎng)絡(luò)營銷經(jīng)驗(yàn),集策劃、開發(fā)、設(shè)計(jì)、營銷、管理等多方位專業(yè)化運(yùn)作于一體。

#T#

在LINQ中,IQueryable 接口和IEnumerable 接口都分別提供了Skip方法和Take方法,用來做分頁非常合適.因此我就想用他們做一個(gè)分頁控件,因?yàn)镮Queryable 是繼承自 IEnumerable 的。因此使用接口僅需要針對后者就可以了。使用的時(shí)候只需提供數(shù)據(jù)源、綁定的GridView的、每頁大小即可?,F(xiàn)在問題就出了在數(shù)據(jù)源上,要求用戶提供一個(gè)數(shù)據(jù)源類型,即IQueryable 接口和IEnumerable 接口? T是可確定類型(已知類型)的話還可以,若T是匿名類型,如

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

list的類型只有在運(yùn)行時(shí)才能得到,怎么辦呢!其實(shí)很簡單我,我們可以使用 “參數(shù)推導(dǎo)泛型類型”的方法來實(shí)現(xiàn):
看下面的代碼(因?yàn)橹攸c(diǎn)不在這里所以 代碼寫的比較粗糙):

 
 
  1. public void BindBoundControl(IEnumerable DataSource, GridView BoundControl, int PageSize)  
  2.         {  
  3.  
  4.             //獲取總記錄數(shù)(這里可以使用參數(shù)傳入總頁數(shù) 就不必每次都執(zhí)行下面方法)  
  5.             int totalRecordCount = DataSource.Count();  
  6.  
  7.             //計(jì)算總頁數(shù)  
  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.             //從參數(shù)中獲取當(dāng)前頁碼  
  24.             int CurrentPageIndex = 1;  
  25.  
  26.             //如果從參數(shù)中獲取頁碼不正確 設(shè)置頁碼為第一頁  
  27. if (!int.TryParse(HttpContext.Current.Request.QueryString["Page"], out CurrentPageIndex) || CurrentPageIndex <= 0 || CurrentPageIndex > totalPageCount)  
  28.             {  
  29.                 CurrentPageIndex = 1;  
  30.             }  
  31.             //綁定數(shù)據(jù)源  
  32.             BoundControl.DataSource = DataSource.Skip((CurrentPageIndex - 1) * PageSize).Take(PageSize);  
  33.             BoundControl.DataBind();  
  34.         } 

調(diào)用

 
 
  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(de.Customers.OrderBy(v=>v.CustomerID), this.GridView1, 10);    

下面我們只是需要重載一下我們的分頁方法實(shí)現(xiàn)“參數(shù)推導(dǎo)泛型類型”就可以了 代碼如下:

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

調(diào)用

 
 
  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);    

這個(gè)方法很簡單的 只是通過 list.FirstOrDefault() 做參數(shù) 來推導(dǎo) 方法中 BindBoundControl 的TSource 就可以了,當(dāng)然因?yàn)槊看畏猪摃r(shí)都會(huì)執(zhí)行 list.FirstOrDefault() 會(huì)損失一點(diǎn)點(diǎn)的效率。


當(dāng)前文章:淺析LINQ通用分頁綁定方法的實(shí)現(xiàn)
網(wǎng)站地址:http://www.dlmjj.cn/article/djjjsij.html