日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
關閉右側工具欄

新聞中心

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

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

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

#T#

在LINQ中,IQueryable 接口和IEnumerable 接口都分別提供了Skip方法和Take方法,用來做分頁非常合適.因此我就想用他們做一個分頁控件,因為IQueryable 是繼承自 IEnumerable 的。因此使用接口僅需要針對后者就可以了。使用的時候只需提供數(shù)據(jù)源、綁定的GridView的、每頁大小即可?,F(xiàn)在問題就出了在數(shù)據(jù)源上,要求用戶提供一個數(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的類型只有在運行時才能得到,怎么辦呢!其實很簡單我,我們可以使用 “參數(shù)推導泛型類型”的方法來實現(xià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.             //計算總頁數(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ù)中獲取當前頁碼  
  24.             int CurrentPageIndex = 1;  
  25.  
  26.             //如果從參數(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.         } 

調用

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

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

 
 
  1. public void BindBoundControl(IEnumerable 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() 做參數(shù) 來推導 方法中 BindBoundControl 的TSource 就可以了,當然因為每次分頁時都會執(zhí)行 list.FirstOrDefault() 會損失一點點的效率。


文章題目:淺析LINQ通用分頁綁定方法的實現(xiàn)
文章URL:http://www.5511xx.com/article/djjjsij.html