微软提供了PagedList分页,相信大家在网上也能搜索一大堆关于pagedList用法的博客,论坛。但是,在使用的过程中一不小心,就会掉入pagedList某种常规用法的陷阱。

我所说的某种常规用法是指如下方法(也可以参考我的博客:PagedList 分页用法):

代码如下:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using System.Linq;
using EF_Test.DAL;
using System.Data;
using PagedList; namespace EF_Test.Controllers
{
public class HomeController : Controller
{
private StudentContext db = new StudentContext();
/// <summary>
/// 简单分页演示
/// </summary>
/// <param name="page">页码</param>
/// <returns></returns>
public ActionResult Index2(int page = )//查询所有学生数据
{
return View(db.Students.OrderBy(item => item.Id).ToPagedList(page, ));
}
}
}

前端HTML

@model PagedList.IPagedList<EF_Test.DAL.Student>
@using PagedList.Mvc
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
} @section css{
<link href="~/Content/PagedList.css" rel="stylesheet" />
<style type="text/css">
body {
font-size: 12px;
font-family: "微软雅黑";
color: #555;
position: relative;
background: #fff;
} a {
text-decoration: none;
color: #555;
} #tbList {
border: 1px solid none;
width: 800px;
margin: 10px auto;
border-collapse: collapse;
} #tbList th, td {
border: 1px solid #ccc;
padding: 5px;
text-align: center;
} tfoot tr td {
border: none;
}
</style>
} @using (Html.BeginForm("Index", "Home", FormMethod.Get))
{
<div style="text-align: center;">
<h1>Mvc分页例子</h1>
<table id="tbList"> <tbody>
@if (Model.Count() != 0)
{
<tr>
<th>姓名
</th>
<th>性别
</th>
<th>学号
</th>
</tr>
foreach (var item in Model)
{
<tr style="text-align: center;">
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Sex)
</td>
<td>
@Html.DisplayFor(modelItem => item.StudentNum)
</td>
</tr>
} }
</tbody>
<tfoot>
<tr>
<td colspan="5">
<div class="">
@if (Model != null)
{
<span style="height: 20px; line-height: 20px;">共 @Model.TotalItemCount.ToString() 条记录,当前第 @Model.PageNumber 页/共 @Model.PageCount 页 </span>
@Html.PagedListPager(Model, page => Url.Action("Index", new { page }), new PagedListRenderOptions() { LinkToFirstPageFormat = "首页", LinkToNextPageFormat = "下一页", LinkToPreviousPageFormat = "上一页", LinkToLastPageFormat = "末页", DisplayItemSliceAndTotal = false, MaximumPageNumbersToDisplay = 3 })
}
</div>
</td>
</tr>
</tfoot>
</table>
</div>
}

上述的用法很简单,直接查询所有数据,然后利用pagedList提供的HTML helper 进行分页。

其效果图也不错,如下:

上述中红色字体提到:该用法需要一次性查询表中所有数据,试问:如果您的数据表中有百万甚至千万条数据,那么这种用法效率是不是将会很低?

说来也惭愧,当初用的时候,我也想到了这个弊端,但是一直没去想办法解决这个问题。

还好,pagedList还提供了另外一种方法:StaticPagedList 方法

StaticPagedList 方法需要提供四个参数,分别为:数据源  当前页码  每页条数  以及总记录数

如上述所言,我们在查询的过程中不能一次性查询所有数据,因为这样做效率很低。而现在我们要做的就是查询当前页码的 10 条数据(假设每页展示十条数据)及返回数据表中的总记录数。

那么我们该怎么做呢?

方法其实很多,在我的项目中,我用到一个存储过程<不管你用什么,你现在要做的就是返回:当前页码的 10 条数据,及数据表总记录条数>

我用到的存储过程为:请参考我的上篇博客

有了存储过程,我们就要用EF执行这个存储过程,怎么执行呢?

接口层:

IEnumerable<StudentModel> GetPagePro(string tableName, string fields, string orderField, string sqlWhere, int pageSize, int pageIndex, out int totalPage, out int RecordCount);

执行层:继承接口

        /// <summary>
/// EF执行存储过程
/// </summary>
/// <param name="tableName">表名</param>
/// <param name="fields">所要查询的字段</param>
/// <param name="orderField">排序字段</param>
/// <param name="sqlWhere">条件语句 where</param>
/// <param name="pageSize">页容量</param>
/// <param name="pageIndex">页码</param>
/// <param name="totalPage">out参数 总分页数量</param>
/// <param name="RecordCount">out 参数 总记录数</param>
/// <returns></returns>
public IEnumerable<StudentModel> GetPagePro(string tableName, string fields, string orderField, string sqlWhere, int pageSize, int pageIndex, out int totalPage, out int RecordCount)
{
using (StudentEntities context = new StudentEntities())
{
SqlParameter[] parameters = {
new SqlParameter("@TableName", SqlDbType.NText),
new SqlParameter("@Fields", SqlDbType.NText),
new SqlParameter("@OrderField", SqlDbType.NText),
new SqlParameter("@sqlWhere", SqlDbType.NText),
new SqlParameter("@pageSize", SqlDbType.Int),
new SqlParameter("@pageIndex", SqlDbType.Int),
new SqlParameter("@TotalPage", SqlDbType.Int),
new SqlParameter("@RecordCount", SqlDbType.Int)
};
parameters[].Value = tableName;
parameters[].Value = fields;
parameters[].Value = orderField;
parameters[].Value = sqlWhere;
parameters[].Value = pageSize;
parameters[].Value = pageIndex;
parameters[].Direction = ParameterDirection.Output;
parameters[].Direction = ParameterDirection.Output;
var data = context.Database.SqlQuery<StudentModel>("exec [ZXL_GetPageData] @TableName,@Fields,@OrderField,@sqlWhere,@pageSize,@pageIndex,@TotalPage out,@RecordCount out", parameters).ToList();
int count = data.Count;
//
string n6 = parameters[].Value.ToString();
string n7 = parameters[].Value.ToString();
//
totalPage = !string.IsNullOrEmpty(n6) ? int.Parse(n6) : ;
RecordCount = !string.IsNullOrEmpty(n7) ? int.Parse(n7) : ;
return data;
}
}

实体Model层:

    public class StudentModel
{
public int Id { get; set; }
public string StuNum { get; set; }
public string deptNum { get; set; }
public string StuName { get; set; }
public string StuSex { get; set; }
public Nullable<System.DateTime> AddTime { get; set; }
}

控制器代码:

        public ActionResult Index(int page=)//查询所有学生数据
{
int totalPage=;
int recordCount=;
var data = studentdb.GetPagePro("Student", "*", "Id", "", , page, out totalPage, out recordCount);
var studentList = new StaticPagedList<StudentModel>(data,page,,recordCount);
return View(studentList);//
}

UI/View层

@model PagedList.StaticPagedList<Test.Model.StudentModel>
@using PagedList.Mvc
@using PagedList
@{
ViewBag.Title = "Index";
Layout = null;
}
<link href="~/Content/PagedList.css" rel="stylesheet" />
<style type="text/css">
body {
font-size: 12px;
font-family: "微软雅黑";
color: #555;
position: relative;
background: #fff;
} a {
text-decoration: none;
color: #555;
} #tbList {
border: 1px solid none;
width: 800px;
margin: 10px auto;
border-collapse: collapse;
} #tbList th, td {
border: 1px solid #ccc;
padding: 5px;
text-align: center;
} tfoot tr td {
border: none;
}
</style> @using (Html.BeginForm("Index", "Home", FormMethod.Get))
{
<div style="text-align: center;">
<h1>Mvc分页例子</h1>
<table id="tbList">
@* <thead>
<tr>
<th>
<input id="StuName" name="StuName" type="text" placeholder="请输入姓名" />
</th>
<th>
<input id="StuNum" name="StuNum" type="text" placeholder="请输入学号" />
</th>
<th>
<input id="Submit1" type="submit" value="submit" />
</th>
</tr>
</thead>*@ <tbody>
@if (Model.Count() != 0)
{
<tr>
<th>姓名
</th>
<th>性别
</th>
<th>学号
</th>
</tr>
foreach (var item in Model)
{
<tr style="text-align: center;">
<td>
@Html.DisplayFor(modelItem => item.StuName)
</td>
<td>
@Html.DisplayFor(modelItem => item.StuSex)
</td>
<td>
@Html.DisplayFor(modelItem => item.StuNum)
</td>
</tr>
} }
</tbody>
<tfoot>
<tr>
<td colspan="5">
<div class="">
@if (Model != null)
{
<span style="height: 20px; line-height: 20px;">共 @Model.TotalItemCount.ToString() 条记录,当前第 @Model.PageNumber 页/共 @Model.PageCount 页 </span>
@Html.PagedListPager(Model, page => Url.Action("Index", new { page }), new PagedListRenderOptions() { LinkToFirstPageFormat = "首页", LinkToNextPageFormat = "下一页", LinkToPreviousPageFormat = "上一页", LinkToLastPageFormat = "末页", DisplayItemSliceAndTotal = false, MaximumPageNumbersToDisplay = 3 })
}
</div>
</td>
</tr>
</tfoot>
</table>
</div>
}

上述代码已经很齐全了,大家可以自行尝试,需要说明两点:

控制器代码:

view层HTML代码:

至此,整个pagedList分页就完毕了。

这样查询提升了效率。

我的分页效果图如下:

由图可知,我的数据表共有:151303条记录,如果采用每次都加载所有数据,效率是何其低可想而知。

呵呵,截止到这儿,pagedlist分页也就讲完了!

现在,我们提出新的要求:结合查询,根据学生姓名和学号进行模糊查询

其后端变更如下:

        public ActionResult Index(int page = , string StuName = "", string StuNum = "",string sortOrder="")//查询所有学生数据
{
string where = string.Empty;
if (!string.IsNullOrEmpty(StuName))
{
ViewBag.StuName = StuName;
where += " and StuName like '%" + StuName + "%'";
}
if (!string.IsNullOrEmpty(StuNum))
{
ViewBag.StuNum = StuNum;
where += " and StuNum like '%" + StuNum + "%'";
}
int totalPage = ;
int recordCount = ;
var data = model.GetPagePro("Student", "*", "Id", " 1=1 " + where, , page, out totalPage, out recordCount); var studentList = new StaticPagedList<StudentModel>(data, page, , recordCount);
return View(studentList);//
}

前端如下:

@model PagedList.StaticPagedList<Test.Model.StudentModel>
@using PagedList.Mvc
@using PagedList
@{
ViewBag.Title = "Index";
Layout = null;
}
<link href="~/Content/PagedList.css" rel="stylesheet" />
<style type="text/css">
body {
font-size: 12px;
font-family: "微软雅黑";
color: #555;
position: relative;
background: #fff;
} a {
text-decoration: none;
color: #555;
} #tbList {
border: 1px solid none;
width: 800px;
margin: 10px auto;
border-collapse: collapse;
} #tbList th, td {
border: 1px solid #ccc;
padding: 5px;
text-align: center;
} tfoot tr td {
border: none;
}
</style> @using (Html.BeginForm("Index", "Home", FormMethod.Get))
{
<div style="text-align: center;">
<h1>Mvc分页例子</h1>
<table id="tbList">
<thead>
<tr>
<th>
<input id="StuName" name="StuName" type="text" placeholder="请输入姓名" value="@ViewBag.StuName" />
</th>
<th>
<input id="StuNum" name="StuNum" type="text" placeholder="请输入学号" value="@ViewBag.StuNum" />
</th>
<th>
<input id="Submit1" type="submit" value="submit" />
</th>
</tr>
</thead> <tbody>
@if (Model.Count() != 0)
{
<tr>
<th>姓名
</th>
<th>性别
</th>
<th>学号
</th>
</tr>
foreach (var item in Model)
{
<tr style="text-align: center;">
<td>
@Html.DisplayFor(modelItem => item.StuName)
</td>
<td>
@Html.DisplayFor(modelItem => item.StuSex)
</td>
<td>
@Html.DisplayFor(modelItem => item.StuNum)
</td>
</tr>
} }
</tbody>
<tfoot>
<tr>
<td colspan="5">
<div class="">
@if (Model != null)
{
<span style="height: 20px; line-height: 20px;">共 @Model.TotalItemCount.ToString() 条记录,当前第 @Model.PageNumber 页/共 @Model.PageCount 页 </span>
@Html.PagedListPager(Model, page => Url.Action("Index", new { page,StuName=ViewBag.StuName,StuNum=ViewBag.StuNum }), new PagedListRenderOptions() { LinkToFirstPageFormat = "首页", LinkToNextPageFormat = "下一页", LinkToPreviousPageFormat = "上一页", LinkToLastPageFormat = "末页", DisplayItemSliceAndTotal = false, MaximumPageNumbersToDisplay = 3 })
}
</div>
</td>
</tr>
</tfoot>
</table>
</div>
}

上图为变更处、

运行效果:

@陈卧龙的博客

EF 利用PagedList进行分页并结合查询 方法2的更多相关文章

  1. ASP.NET MVC利用PagedList分页(一)

    前几天看见博客园上有人写ASP.NET MVC的分页思想,这让我不禁想起了PagedList.PagedList是NuGet上提供的一个分页的类库,能对任何IEnumerable<T>进行 ...

  2. Webform(分页、组合查询)

    一.分页 1.写查询方法: public List<Student> Select(int PageCount, int PageNumber) {//PageCount为每页显示条数,P ...

  3. ASP.NET MVC利用PagedList分页(二)PagedList+Ajax+JsRender

    (原文) 昨天在ASP.NET MVC利用PagedList分页(一)的 最后一节提到,一个好的用户体验绝对不可能是点击下一页后刷新页面,所以今天来说说利用Ajax+PagedList实现无刷新(个人 ...

  4. ASP.NET MVC + EF 利用存储过程读取大数据,1亿数据测试很OK

    看到本文的标题,相信你会忍不住进来看看! 没错,本文要讲的就是这个重量级的东西,这个不仅仅支持单表查询,更能支持连接查询, 加入一个表10W数据,另一个表也是10万数据,当你用linq建立一个连接查询 ...

  5. ASP.NET MVC + EF 利用存储过程读取大数据

    ASP.NET MVC + EF 利用存储过程读取大数据,1亿数据测试很OK 看到本文的标题,相信你会忍不住进来看看! 没错,本文要讲的就是这个重量级的东西,这个不仅仅支持单表查询,更能支持连接查询, ...

  6. 【1】MySQL大数据量分页查询方法及其优化

    ---方法1: 直接使用数据库提供的SQL语句---语句样式: MySQL中,可用如下方法: SELECT * FROM 表名称 LIMIT M,N---适应场景: 适用于数据量较少的情况(元组百/千 ...

  7. MySQL大数据量分页查询方法及其优化

    MySQL大数据量分页查询方法及其优化   ---方法1: 直接使用数据库提供的SQL语句---语句样式: MySQL中,可用如下方法: SELECT * FROM 表名称 LIMIT M,N---适 ...

  8. 利用SqlDataAdapter进行分页

    利用SqlDataAdapter进行记录分页 说到分页,很多地方都会用到,不管是windows程序还是web程序,为什么要进行分页?很简单,如果BlueIdea BBS帖子列表不分页的话,几十万条记录 ...

  9. ajax分页与组合查询配合使用

    使用纯HTML页与js.ajax.Linq实现分页与组合查询的配合使用 <body> <div id="top"><input type=" ...

随机推荐

  1. 二层协议--STP协议总结

    生成树协议的技术实现与配置注意点 一.stp协议的用途 二.stp协议的运行机制 三.stp协议规范

  2. 网络基础 cookie详解

    cookie详解 by:授客 QQ:1033553122 cookie干嘛用的? 参见文章http 会话(session)详解: 网络基础 http 会话(session)详解   cookie分类 ...

  3. Java并发编程(十)阻塞队列

    使用非阻塞队列的时候有一个很大问题就是:它不会对当前线程产生阻塞,那么在面对类似消费者-生产者的模型时,就必须额外地实现同步策略以及线程间唤醒策略,这个实现起来就非常麻烦.但是有了阻塞队列就不一样了, ...

  4. JAVA 设计模式遵循的六大基本准则

    JAVA 设计模式遵循的六大基本准则 一.单一职责原则:(Single Responsibility Pinciple)  一个类只负责一项职责. 当超过一项职责需要负责时,需要增加新的类来负责新的职 ...

  5. maven(九),install安装到本地仓库

    下载oracle驱动jar包 在maven中央仓库中,是没有oracle驱动jar包的.因为oracle是商业软件,其jar包不允许用作开源用途.从http://www.mvnrepository.c ...

  6. Python创建空DataFrame及添加行数据

    # 创建空DataFrame df = pd.DataFrame(columns = ['YJML','EJML','SJML','WZLB','GGXHPZ','CGMS']) # 插入数据(忽略索 ...

  7. 洗礼灵魂,修炼python(10)--有趣的判断分支+从实例中掌握循环语句

    所有的编程语言里都有判断语句和循环语句. 判断语句则是用来分支程序流程的 循环语句则是为了实现一个效果,让程序的规律性的重复操作 不用说,分支和循环自然在python里也是有的 一,条件判断:if,i ...

  8. C++ Standards Support in GCC - GCC 对 C++ 标准的支持

    C++ Standards Support in GCC - 2019-2-20 GCC supports different dialects of C++, corresponding to th ...

  9. window下上传文件至linux(windows下如何访问linux)

    ========相信我,按照步骤来一定能成功====== 我将从三个方面来说明:为什么要搭建访问服务器.如何搭建访问服务器.windows如下访问 为什么要搭建访问Linux服务器 我们都知道,服务器 ...

  10. MATLAB插 值 法

    MATLAB插  值  法 作者:凯鲁嘎吉 - 博客园http://www.cnblogs.com/kailugaji/ 一.实验目的 二.实验原理 三.实验程序 四.实验内容 五.解答 1. 程序 ...