发布:2023/12/7 15:52:13作者:大数据 来源:大数据 浏览次数:626
利用EF的skip和take实现性能分页,随着数据增多,比如50万,使用x.pagelist插件分页太慢。
代码示例:
1 2 3 4 5 6 7 8 9 10 11 |
var q = _dbcontext.Company.AsQueryable(); page = page ?? 1; pageSize = pageSize ?? 10; int count = q.Count(); ViewBag.Page = page; ViewBag.PageSize = pageSize; ViewBag.TotalCount = count; ViewBag.TotalPage = (int)Math.Ceiling(count / (double)pageSize); q = q.OrderByDescending(o => o.Id).Skip(((int)page - 1) * (int)pageSize).Take((int)pageSize); return View(q); |
前台view
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<div class="col-lg-12"> @{ var pageIndex = (int)ViewBag.Page; var pageSize = (int)ViewBag.PageSize; var totalPage = (int)ViewBag.TotalPage; var totalCount = (int)ViewBag.TotalCount; var homeDisabled = pageIndex > 1 ? "" : "disabled"; var prevDisabled = pageIndex > 1 ? "" : "disabled"; var nextDisabled = pageIndex >= totalPage ? "disabled" : ""; var lastDisabled = pageIndex >= totalPage ? "disabled" : ""; var allRoutes = new Dictionary<string, string>(); allRoutes.Add("Keywords", ViewBag.Keywords); allRoutes.Add("Industry", ViewBag.Industry); allRoutes.Add("CompanyType", ViewBag.CompanyType); allRoutes.Add("BusinessScope", ViewBag.BusinessScope); allRoutes.Add("LegalRepresentative", ViewBag.LegalRepresentative); allRoutes.Add("BusinessStatus", ViewBag.BusinessStatus); } <span class="float-left" style="font-size:14px;padding:6px 10px 6px 0px;">共 @ViewBag.TotalCount 条</span> <select id="pageSizeList" class="form-control float-left" style="width:100px;"> <option value="10" selected>10 条/页</option><option value="20">20 条/页</option><option value="30">30 条/页</option> <option value="40">40 条/页</option><option value="50">50 条/页</option><option value="60">60 条/页</option> <option value="70">70 条/页</option><option value="80">80 条/页</option><option value="90">90 条/页</option></select> <div class="clearfix float-right"> <div class="btn-group"> <a asp-action="Index" asp-all-route-data="@allRoutes" asp-route-page="1" asp-route-pageSize=@ViewBag.PageSize class="btn btn-default @homeDisabled"> 首页 </a> <a asp-action="Index" asp-all-route-data="@allRoutes" asp-route-page="@(pageIndex - 1)" asp-route-pageSize=@ViewBag.PageSize class="btn btn-default @prevDisabled"> 上一页 </a> <a asp-action="Index" asp-all-route-data="@allRoutes" asp-route-page="@(pageIndex + 1)" asp-route-pageSize=@ViewBag.PageSize class="btn btn-default @nextDisabled"> 下一页 </a> <a asp-action="Index" asp-all-route-data="@allRoutes" asp-route-page="@totalPage" asp-route-pageSize=@ViewBag.PageSize class="btn btn-default @lastDisabled"> 末页 </a> </div> <span class="btn btn-info ml-5">第 @pageIndex / @totalPage 页</span> </div> </div> |
具体根据情况修改代码
© Copyright 2014 - 2024 柏港建站平台 ejk5.com. 渝ICP备16000791号-4