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

創(chuàng)新互聯(lián)公司主要從事網(wǎng)頁設(shè)計、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è)計、網(wǎng)站設(shè)計、網(wǎng)絡(luò)營銷經(jīng)驗,集策劃、開發(fā)、設(shè)計、營銷、管理等多方位專業(yè)化運作于一體。
#T#
在LINQ中,IQueryable
- var list = from it in de.Customers where it.City == "abc" select new { it.City, it.Country };
list的類型只有在運行時才能得到,怎么辦呢!其實很簡單我,我們可以使用 “參數(shù)推導(dǎo)泛型類型”的方法來實現(xiàn):
看下面的代碼(因為重點不在這里所以 代碼寫的比較粗糙):
- public void BindBoundControl
(IEnumerable DataSource, GridView BoundControl, int PageSize) - {
- //獲取總記錄數(shù)(這里可以使用參數(shù)傳入總頁數(shù) 就不必每次都執(zhí)行下面方法)
- int totalRecordCount = DataSource.Count();
- //計算總頁數(shù)
- int totalPageCount = 0;
- if (PageSize == 0)
- {
- PageSize = totalRecordCount;
- }
- if (totalRecordCount % PageSize == 0)
- {
- totalPageCount = totalRecordCount / PageSize;
- }
- else
- {
- totalPageCount = totalRecordCount / PageSize + 1;
- }
- //從參數(shù)中獲取當(dāng)前頁碼
- int CurrentPageIndex = 1;
- //如果從參數(shù)中獲取頁碼不正確 設(shè)置頁碼為第一頁
- if (!int.TryParse(HttpContext.Current.Request.QueryString["Page"], out CurrentPageIndex) || CurrentPageIndex <= 0 || CurrentPageIndex > totalPageCount)
- {
- CurrentPageIndex = 1;
- }
- //綁定數(shù)據(jù)源
- BoundControl.DataSource = DataSource.Skip((CurrentPageIndex - 1) * PageSize).Take(PageSize);
- BoundControl.DataBind();
- }
調(diào)用
- 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); - }
下面我們只是需要重載一下我們的分頁方法實現(xiàn)“參數(shù)推導(dǎo)泛型類型”就可以了 代碼如下:
- public void BindBoundControl
(IEnumerable DataSource, TSource type, GridView BoundControl, int PageSize) - {
- this.BindBoundControl(DataSource, BoundControl, PageSize);
- }
調(diào)用
- 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() 做參數(shù) 來推導(dǎo) 方法中 BindBoundControl
分享標(biāo)題:淺析LINQ通用分頁綁定方法的實現(xiàn)
文章網(wǎng)址:http://fisionsoft.com.cn/article/djjjsij.html


咨詢
建站咨詢
