EntityFramework进阶(五)- 分页
本系列原创博客代码已在EntityFramework6.0.0测试通过,转载请标明出处
我们创建分页信息类CommonPagedList,包含了字段总条数,总页数,当前页码,页大小,当前页数据。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; /*
* CopyRight ©2017 All Rights Reserved
* 作者:Rex Sheng
*/
namespace SuperNet.EntityFramework.PagedList
{
/// <summary>
/// 共通分页类
/// </summary>
/// <typeparam name="T"></typeparam>
public class CommonPagedList<T> : List<T>
where T : class
{
public int TotalItemCount { get; set; } public int PageSize { get; set; } public int PageIndex { get; set; } public int TotalPageCount { get; set; } private CommonPagedList() { } public CommonPagedList(CommonPagedList<T> list)
{
this.PageIndex = list.PageIndex;
this.PageSize = list.PageSize;
this.TotalItemCount = list.TotalItemCount;
this.TotalPageCount = list.TotalPageCount;
AddRange(list);
} public CommonPagedList(IQueryable<T> list, int pageIndex, int pageSize)
{
if (pageIndex <= )
pageIndex = ;
this.TotalItemCount = list.Count();
this.PageSize = pageSize;
this.TotalPageCount = this.TotalItemCount % this.PageSize == ? this.TotalItemCount / this.PageSize : this.TotalItemCount / this.PageSize + ;
pageIndex = pageIndex > this.TotalPageCount ? this.TotalPageCount : pageIndex;
this.PageIndex = pageIndex;
if (this.PageIndex > this.TotalPageCount)
this.PageIndex = this.TotalPageCount;
if (pageIndex <= )
{
pageIndex = ;
}
if (this.PageIndex <= )
{
this.PageIndex = ;
} var query = list.Skip((pageIndex - ) * pageSize).Take(pageSize);
AddRange(query);
} public CommonPagedList(IEnumerable<T> list, int pageIndex, int pageSize, int totalItemCount)
{
if (pageIndex <= )
pageIndex = ;
this.TotalItemCount = totalItemCount;
this.TotalPageCount = totalItemCount % pageSize == ? totalItemCount / pageSize : totalItemCount / pageSize + ;
pageIndex = pageIndex > this.TotalPageCount ? this.TotalPageCount : pageIndex;
if (pageIndex <= )
pageIndex = ;
this.PageIndex = pageIndex;
this.PageSize = pageSize;
AddRange(list);
}
}
}
调用的时候,我们创建扩展方法来实现
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks; /*
* CopyRight ©2017 All Rights Reserved
* 作者:Rex Sheng
*/
namespace SuperNet.EntityFramework.PagedList
{
public static class PagedListExtension
{
/// <summary>
/// 共通分页类扩展
/// </summary>
/// <typeparam name="T">查询返回类</typeparam>
/// <param name="allItems">要查询的数据</param>
/// <param name="pageIndex">要查询的页码</param>
/// <param name="pageSize">每页大小</param>
/// <returns></returns>
public static CommonPagedList<T> ToCommonPagedList<T>(this IOrderedQueryable<T> allItems, int pageIndex, int pageSize) where T : class
{
CommonPagedList<T> Result = new CommonPagedList<T>(allItems, pageIndex, pageSize);
return Result;
} /// <summary>
/// 单个字段排序分页查询
/// </summary>
/// <typeparam name="T">查询返回类</typeparam>
/// <typeparam name="TKey">排序字段类型</typeparam>
/// <param name="allItems">要查询的数据</param>
/// <param name="orderator">排序字段表达式</param>
/// <param name="asc">是否升序</param>
/// <param name="pageIndex">要查询的页码</param>
/// <param name="pageSize">每页大小</param>
/// <returns></returns>
public static CommonPagedList<T> ToCommonPagedList<T, TKey>(this IQueryable<T> allItems, Expression<Func<T, TKey>> orderator, bool asc, int pageIndex, int pageSize) where T : class
{
var source = asc ? allItems.OrderBy(orderator) : allItems.OrderByDescending(orderator);
CommonPagedList<T> Result = new CommonPagedList<T>(source, pageIndex, pageSize);
return Result;
} /// <summary>
/// 多个字段排序分页查询
/// </summary>
/// <typeparam name="T">查询返回类</typeparam>
/// <typeparam name="TKey1">排序字段类型1</typeparam>
/// <typeparam name="TKey2">排序字段类型2</typeparam>
/// <param name="allItems">要查询的数据</param>
/// <param name="orderator1">排序字段表达式1</param>
/// <param name="orderator2">排序字段表达式2</param>
/// <param name="asc1">是否升序1</param>
/// <param name="asc2">是否升序2</param>
/// <param name="pageIndex">要查询的页码</param>
/// <param name="pageSize">每页大小</param>
/// <returns></returns>
public static CommonPagedList<T> ToCommonPagedList<T, TKey1, TKey2>(this IQueryable<T> allItems, Expression<Func<T, TKey1>> orderator1, Expression<Func<T, TKey2>> orderator2, bool asc1, bool asc2, int pageIndex, int pageSize) where T : class
{
var source = asc1 ? (asc2 ? allItems.OrderBy(orderator1).ThenBy(orderator2) : allItems.OrderBy(orderator1).ThenByDescending(orderator2))
: (asc2 ? allItems.OrderByDescending(orderator1).ThenBy(orderator2) : allItems.OrderByDescending(orderator1).ThenByDescending(orderator2));
CommonPagedList<T> Result = new CommonPagedList<T>(source, pageIndex, pageSize);
return Result;
} /// <summary>
/// 共通分页类
/// </summary>
/// <typeparam name="T">查询返回类</typeparam>
/// <param name="allItems">要查询的数据</param>
/// <param name="pageIndex">要查询的页码</param>
/// <param name="pageSize">每页大小</param>
/// <returns></returns>
public static CommonPagedList<T> ToCommonPagedList<T>(this IOrderedEnumerable<T> allItems, int pageIndex, int pageSize) where T : class
{
CommonPagedList<T> Result = new CommonPagedList<T>(allItems.AsQueryable(), pageIndex, pageSize);
return Result;
}
}
}
EntityFramework进阶(五)- 分页的更多相关文章
- J2EE进阶(五)Spring在web.xml中的配置
J2EE进阶(五)Spring在web.xml中的配置 前言 在实际项目中spring的配置文件applicationcontext.xml是通过spring提供的加载机制自动加载到容器中.在web ...
- Web Service进阶(五)SOAPBinding方式讲解
Web Service进阶(五)SOAPBinding方式讲解 Java API for XML Web Services (JAX-WS) 2.0 (JSR 224) Standard Implem ...
- Java进阶(五十三)屡试不爽之正则表达式
Java进阶(五十三)屡试不爽之正则表达式 在线测试网址: http://tool.oschina.net/regex/# 上面的在线测试网址,含有正则表达式的生成,非常实用.大家共勉之! 匹配中文: ...
- Java进阶(五十一)Could not create the view: An unexpected exception was thrown
Java进阶(五十一)Could not create the view: An unexpected exception was thrown 今天打开Myeclipse10的时候,发现server ...
- Java进阶(五十一)必须记住的Myeclipse快捷键
Java进阶(五十一)必须记住的Myeclipse快捷键 在调试程序的时候,我们经常需要注释一些代码,在用Myeclipse编程时,就可以用 Ctrl+/ 为选中的一段代码加上以 // 打头的注释:当 ...
- Java进阶(五十二)利用LOG4J生成服务日志
Java进阶(五十二)利用LOG4J生成服务日志 前言 由于论文写作需求,需要进行流程挖掘.前提是需要有真实的事件日志数据.真实的事件日志数据可以用来发现.监控和提升业务流程. 为了获得真实的事件日志 ...
- Python爬虫进阶五之多线程的用法
前言 我们之前写的爬虫都是单个线程的?这怎么够?一旦一个地方卡到不动了,那不就永远等待下去了?为此我们可以使用多线程或者多进程来处理. 首先声明一点! 多线程和多进程是不一样的!一个是 thread ...
- Python进阶(五)----内置函数Ⅱ 和 闭包
Python进阶(五)----内置函数Ⅱ 和 闭包 一丶内置函数Ⅱ ####内置函数#### 特别重要,反复练习 ###print() 打印输入 #sep 设定分隔符 # end 默认是换行可以打印到 ...
- EntityFramework进阶(二)- DbContext预热
本系列原创博客代码已在EntityFramework6.0.0测试通过,转载请标明出处 在DbContext首次调用的时候,会很慢,甚至会有5,6秒的等待,通常称为冷查询.再次调用的时候,几毫秒就能请 ...
- MySQL进阶8 分页查询(limit) - 【SQL查询语法执行顺序及大致结构】- 子查询的3个经典案例
#进阶8 分页查询 /* 应用场景: 当要显示的数据,一页显示不全,需要分页提交sql请求 语法: select 查询列表 #7 from 表1 #执行顺序:#1 [join type join 表2 ...
随机推荐
- pytorch nn.Sequential()动态添加方法
之前我们使用nn.Sequential()都是直接写死的,就如下所示: # Example of using Sequential model = nn.Sequential( nn.Conv2d(, ...
- 泡泡一分钟:FMD Stereo SLAM: Fusing MVG and Direct Formulation Towards Accurate and Fast Stereo SLAM
FMD Stereo SLAM: Fusing MVG and Direct Formulation Towards Accurate and Fast Stereo SLAM FMD Stereo ...
- 泡泡一分钟:Aided Inertial Navigation: Unified Feature Representations and Observability Analysis
http://udel.edu/~yuyang/downloads/tr_observabilityII.pdf Aided Inertial Navigation: Unified Feature R ...
- rf安装对应requests库的方法
先要安装requests,再安装requestsLibrary pip install requests pip install robotframework-requests github地址 ht ...
- Python - Django - session 的基本使用
urls.py: from django.conf.urls import url from app02 import views urlpatterns = [ # app02 url(r'^app ...
- centos7上安装mysql8(上)
1.删除系统现存的mysql rpm -pa | grep mysql 2.卸载mysql组件 yum remove mysql-xxx-xxx- 3.删除mysql的配置文件,卸载不会自动删除配置文 ...
- 【相机篇】从到FlyCapture2到Spinnaker
从FlyCapture2 到 Spinnaker SDK的变换,可参见FLIR公司机器视觉的相机产品:https://www.flir.com/iis/machine-vision/ Spinnake ...
- [ kvm ] 学习笔记 8:Ovirt 基础及使用
目录- 1. oVirt 功能介绍- 2. oVirt 安装部署 - 2.1 基础准备 - 2.2 安装 ovirt-engine - 2.3 配置 kvm 主机 - 2.4 ...
- VUE中事件修饰符:stop prevent self capture
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...
- Win10利用CodeBlocks搭建Objective-C开发环境(一)
为了学习ios开发,而手头没有苹果机,若在windows平台下学习objective-c编程.则需要安装OC开发环境, 经过在网上查阅各种相关资料,历经多次失败,终于安装并测试成功,特将安装过程和经验 ...