日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

在 asp.net mvc中的简单分页算法 (续)

發(fā)布時(shí)間:2025/3/19 asp.net 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在 asp.net mvc中的简单分页算法 (续) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在上個月發(fā)表的 http://www.cnblogs.com/bwangel/p/mvcpager.html?中,討論了一下asp.net mvc中結(jié)合Entity framework框架進(jìn)行的分頁,包括服務(wù)端實(shí)現(xiàn)和客戶端的分部視圖的實(shí)現(xiàn)。在一個月過后,發(fā)現(xiàn)服務(wù)器端的代碼每次都寫一堆,非常冗長,基于職業(yè)習(xí)慣,想著把服務(wù)器端的分頁代碼也能封裝一下,這是以前的代碼:

const int PAGE_SZ = 10;[Route("{id:int}/{page:int=1}")]public ActionResult Index(int? id, int? page){var articles = (id == null || id == 0) ? _db.Articles : _db.Articles.Where (art => art.CatalogId == id);var recordCount = articles.Count();articles = articles.OrderByDescending(art => art.EditTime). Skip((page.Value - 1) * PAGE_SZ).Take(PAGE_SZ);ViewBag.Pager = new Pager(){PageIndex = page.Value,PageSize = PAGE_SZ,RecordCount = recordCount,};return View(articles); }

經(jīng)過一翻折騰,在原有的Pager類中增加一個static方法:

/// <summary>/// 根據(jù)指定的集合表達(dá)式和排序字段得出一個分頁對象和分頁后的數(shù)據(jù)集/// </summary>/// <typeparam name="T">實(shí)體的類型</typeparam>/// <typeparam name="TSort">用于排序的屬性類型</typeparam>/// <param name="allList">初始的集合</param>/// <param name="orderExpress">排序的表達(dá)式</param>/// <param name="page">頁號</param>/// <param name="pageSize">頁數(shù)</param>/// <returns></returns>public static Pager GetPagedList<T, TSort>(ref IQueryable<T> allList,Expression<Func<T, TSort>> orderExpress, int? page, int pageSize = 20){int p = Math.Max(1, page == null ? 1 : page.Value);var recordCount = allList.Count();allList = allList.OrderByDescending(orderExpress).Skip((p - 1) * pageSize).Take(pageSize);return new Pager(){PageIndex = p,PageSize = pageSize,RecordCount = recordCount,};}

這樣,上述應(yīng)用層的調(diào)用就簡化為:

public ActionResult Index(int? id, int? page){var articles = (id == null || id == 0) ? _db.Articles : _db.Articles.Where(art => art.CatalogId == id); //此方法要返回兩個值,一個是分頁對象,一個是分頁后的集合。所以后者用ref傳地址。 art=>art.Id是表示根據(jù)Id排序.
ViewBag.Pager
= Pager.GetPagedList(ref articles, art => art.Id, page);return View(articles);}

雖然GetPagerList方法有兩個泛型類型參數(shù),但根據(jù)編譯器的智能化處理,我們在調(diào)用時(shí)可以省略顯示聲明T和TSort,讓編譯器自動判斷。
這樣調(diào)用顯得非常簡潔。

當(dāng)然此方法還可以做進(jìn)一步封裝,把條件查詢表達(dá)式也一起封進(jìn)去,但那樣方法的定義會顯得非常復(fù)雜和冗長,以現(xiàn)有的應(yīng)用規(guī)模不劃算,暫時(shí)沒這必要。

?

經(jīng)過了如此多的封裝,Entity Framework是否能足夠聰明,能高效智能地查詢出我們想要的結(jié)果呢?我不太放心,特地跟蹤了一下,發(fā)現(xiàn)

它和我們預(yù)期的表現(xiàn)完全一致,就兩條語句,一條是整體計(jì)數(shù),一條是具體的分頁:

?

SELECT [GroupBy1].[A1] AS [C1]FROM ( SELECT COUNT(1) AS [A1]FROM [dbo].[CT_Article] AS [Extent1]) AS [GroupBy1]SELECT TOP (20) [Extent1].[Id] AS [Id], [Extent1].[ParentId] AS [ParentId], [Extent1].[Title] AS [Title], [Extent1].[Text] AS [Text], [Extent1].[Intro] AS [Intro], [Extent1].[CatalogId] AS [CatalogId], [Extent1].[AuthorId] AS [AuthorId], [Extent1].[ReplyCount] AS [ReplyCount], [Extent1].[HitCount] AS [HitCount], [Extent1].[PostTime] AS [PostTime], [Extent1].[LastReplyTime] AS [LastReplyTime], [Extent1].[Status] AS [Status]FROM ( SELECT [Extent1].[Id] AS [Id], [Extent1].[ParentId] AS [ParentId], [Extent1].[Title] AS [Title], [Extent1].[Text] AS [Text], [Extent1].[Intro] AS [Intro], [Extent1].[CatalogId] AS [CatalogId], [Extent1].[AuthorId] AS [AuthorId], [Extent1].[ReplyCount] AS [ReplyCount], [Extent1].[HitCount] AS [HitCount], [Extent1].[PostTime] AS [PostTime], [Extent1].[LastReplyTime] AS [LastReplyTime], [Extent1].[Status] AS [Status], row_number() OVER (ORDER BY [Extent1].[Id] DESC) AS [row_number]FROM [dbo].[CT_Article] AS [Extent1]) AS [Extent1]WHERE [Extent1].[row_number] > 0ORDER BY [Extent1].[Id] DESC

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/bwangel/p/mvcpager2.html

總結(jié)

以上是生活随笔為你收集整理的在 asp.net mvc中的简单分页算法 (续)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。