Entity FrameWork 实现分页
SQl语句进行分页
SQL语句进行分页主要是应用Entity FrameWork的SqlQuery()传入SQL语句进行查询时分页。
效果展示。

页面代码展示,显示是用Repeater控件进行动态显示
<%@ Page Title="" Language="C#" MasterPageFile="~/HoTai/HoTaiGuanLi.Master" AutoEventWireup="true" CodeBehind="HoTaiYingYYM.aspx.cs" Inherits="OLMS.HoTai.HoTaiYingYYM" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<link href="../Assets/css/input.css" rel="stylesheet" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div class="pure-form">
<fieldset>
<legend>后台管理<i class="fa fa-angle-double-right"></i>类型管理
<asp:Button ID="Button1" CssClass=" pure-button pure-button-primary tools-button" runat="server" Text="添加音乐类型" OnClick="Button1_Click" />
</legend>
</fieldset>
</div>
<div>
<table class="table table-striped">
<thead>
<tr>
<th scope="col">编号</th>
<th scope="col">应用名称</th>
<th scope="col">类型名称</th>
<th scope="col">歌手名称</th>
<th scope="col">歌手价格</th>
<th scope="col">操作</th>
</tr>
</thead>
<tbody>
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
<ItemTemplate>
<tr>
<th scope="row"><%# Eval("AlbumId") %></th> //表的id
<td><%# Eval("Title") %></td>
<td><%# Eval("Genres.Name") %></td>
<td><%# Eval("Artists.Name") %></td>
<td><%# Eval("Price") %></td>
<td>
<asp:LinkButton ID="LinkButton1" CssClass="btn btn-primary" CommandArgument='<%# Eval("AlbumId") %>' CommandName="dianjia" runat="server">编辑</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" CssClass="btn btn-light" CommandArgument='<%# Eval("AlbumId") %>' CommandName="delete" runat="server">删除</asp:LinkButton></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</tbody>
</table>
</div>
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button2" runat="server" Text="首页" OnClick="Button2_Click" />
<asp:Button ID="Button3" runat="server" Text="上一页" OnClick="Button3_Click" />
<asp:Button ID="Button4" runat="server" Text="下一页" OnClick="Button4_Click" />
<asp:Button ID="Button5" runat="server" Text="末页" OnClick="Button5_Click" />
</div>
</asp:Content>
后台事件代码
using OLMS.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace OLMS.HoTai
{
public partial class HoTaiYingYYM : System.Web.UI.Page
{
static int x = 0;
static int i = 1;
static int zyes;
static int ztian;
static int tianos = 5;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//刷新
Select();
}
}
private void Select()
{
using (OLMSDBEntities db = new OLMSDBEntities())
{
string sql = $"select * from Albums order by AlbumId offset {x} rows fetch next 5 rows only";
Repeater1.DataSource = db.Albums.SqlQuery(sql).ToList();
Repeater1.DataBind();
var fy = db.Albums.ToList();
ztian = fy.Count;
zyes = ztian / tianos;
Label1.Text = $"每页5条/共{ztian}条 第{i}页/共{zyes + 1}页";
}
}
protected void Button2_Click(object sender, EventArgs e)
{
//首页
x = 0;
i = 1;
Select();
}
protected void Button4_Click(object sender, EventArgs e)
{
//下一页
x += 5;
i++;
if (i >= zyes)
{
x = zyes * 5;
i = zyes + 1;
}
if (i >= (zyes + 1))
{
string strUrl = "<script>alert('已到末页');</script>";
Response.Write(strUrl);
}
else
{
Select();
}
}
protected void Button3_Click(object sender, EventArgs e)
{
//上一页
x -= 5;
i--;
if (x < 0)
{
x = 0;
i = 1;
}
if (x <= 0)
{
string strUrl = "<script>alert('已到第一页');</script>";
Response.Write(strUrl);
}
else
{
Select();
}
}
protected void Button5_Click(object sender, EventArgs e)
{
//末页
x = zyes * 5;
i = zyes + 1;
Select();
}
}
}
Skip().Take()进行分页
Skip().Take()进行分页是应用Entity FrameWork的Skip(起始行).Take(每页多少行)方法来进行分页

页面结构和上面是一样的,后台事件代码有区别
using OLMS.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace OLMS.HoTai
{
public partial class HoTaiYingYYM : System.Web.UI.Page
{
static int i = 1; //起始页数
static int zyes; //总条数
static int ztian;//总页数
static int tianos = 5; //每页多少条
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//刷新
Select();
}
}
private void Select()
{
using (OLMSDBEntities db = new OLMSDBEntities())
{
var fy = db.Albums.ToList();
ztian = fy.Count;
zyes = ztian / tianos;
//起始行数 每页多少条
Repeater1.DataSource = db.Albums.ToList().Skip((i - 1) * tianos).Take(tianos).ToList();
Repeater1.DataBind();
Label1.Text = $"每页{tianos}条/共{fy.Count}条 第{i}页/共{(fy.Count / 5) + 1}页";
}
}
protected void Button2_Click(object sender, EventArgs e)
{
i = 1;
Select();
}
protected void Button4_Click(object sender, EventArgs e)
{
i++;
if (i >= zyes)
{
i = zyes + 1;
}
if (i >= (zyes + 1))
{
string strUrl = "<script>alert('已到末页');</script>";
Response.Write(strUrl);
}
else
{
Select();
}
}
protected void Button3_Click(object sender, EventArgs e)
{
i--;
if (i < 0)
{
i = 1;
}
if (i <= 0)
{
string strUrl = "<script>alert('已到第一页');</script>";
Response.Write(strUrl);
}
else
{
Select();
}
}
protected void Button5_Click(object sender, EventArgs e)
{
i = zyes + 1;
Select();
}
}
}
总结
Skip().Take()进行分页方法和SQl语句进行分页总体代码差不太多,按具体需要来进行选择。
Entity FrameWork 实现分页的更多相关文章
- entity framework 新手入门篇(3)-entity framework实现orderby,count,groupby,like,in,分页等
前面我们已经学习了entityframework的基本的增删改查,今天,我们将在EF中实现一些更加贴近于实际功能的SQL方法. 承接上面的部分,我们有一个叫做House的数据库,其中包含house表和 ...
- Entity Framework后台采用分页方式取数据与AspNetPager控件的使用
本文是一个对AspNetPager控件使用的笔记! 有关AspNetPager控件可以查看杨涛主页.这是一个开放的自定义ASP.NET控件,支持各种自定义的数据分页方式,使用很方便,而且功能也很强大, ...
- 基于Entity Framework的自定义分页,增删改的通用实现
简介 之前写个一个基于Dapper的分页实现,现在再来写一个基于Entity Framework的分页实现,以及增删改的通用实现. 代码 还是先上代码:https://github.com/jinwe ...
- Entity Framework分页扩展
Entity Framework分页在我初入门时总是困扰这我,无论是SQL分页还是Entity Framework的分页,总是显得那么麻烦,因此对于Entity Framework单独封装了分页. 一 ...
- 《Entity Framework 6 Recipes》中文翻译系列 (17) -----第三章 查询之分页、过滤和使用DateTime中的日期部分分组
翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 3-12 分页和过滤 问题 你想使用分页和过滤来创建查询. 解决方案 假设你有如图3 ...
- [转]在Entity Framework中使用LINQ语句分页
本文转自:http://diaosbook.com/Post/2012/9/21/linq-paging-in-entity-framework 我们知道,内存分页效率很低.并且,如果是WebForm ...
- Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio » 排序、筛选、分页以及分组
Sorting, filtering, paging, and grouping 7 of 8 people found this helpful By Tom Dykstra The Contoso ...
- Entity Framework 学习之--Ling to entity实现分页
最近用MVC做的一个项目涉及到分页,中间用了entity framework来查数据库,不用直接写sql语句,方便了很多. 一般分页的思路是获得两个变量的值: 1.一共有多少条记录 totalCoun ...
- ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第五章:排序、分页和路由
本章的重点是对产品信息增加排序和分页的功能,以及使用ASP.NET Routing特性添加更加友好的URL支持. 注意:如果你想按照本章的代码编写示例,你必须完成第四章或者直接从www.apress. ...
随机推荐
- 如何将项目上传至GitHub?
心血来潮的一天,突然想写点什么哈哈哈哈. 那就写写如何将项目上传到GitHub(矫情,上传个项目还要写个文章) 第一步:下载Git https://git-scm.com/download/win 下 ...
- 【Linux】1 创建目录:mkdir
mkdir命令用于创建目录,如同一路径下创建单个或多个目录.递归创建目录,但同路径下不能创建同名目录,且目录名区分大小写. [命令] mkdir [用途] 创建目录(单个目录/多个目录) [语法] m ...
- ASP.Net Core中使用Swagger
我们先简单介绍下什么是Swagger,主要是用来干嘛?? 在Swagger诞生之前,我们通常在开发接口的过程中,需要前后端共同维护一个接口文档,然后大家按照接口文档的规范进行对接.接口文档俨然成了接口 ...
- LeetCode寻找两个有序数组的中位数
题目: 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1 和 nu ...
- 【原理探究】女朋友问我ArrayList遍历时删除元素的正确姿势是什么?
简介 我们在项目开发过程中,经常会有需求需要删除ArrayList中的某个元素,而使用不正确的删除方式,就有可能抛出异常.或者在面试中,会遇到面试官询问遍历时如何正常删除元素.所以在本篇文章中,我们会 ...
- Rasa init报错:AttributeError: type object 'Callable' has no attribute '_abc_registry'
错误:Rasa init --no-prompt 报错 原因:Python升级到3.7后会遇到该问题 解决:pip uninstall typing
- javascript----放大模式
放大模式 1.介绍:放大模式降低模块之间直接的联系,降低耦合,当一个模块出现错误,不会影响另一个模块的功能
- RBAC在Django中基于中间件的AJAX应用案例
项目文件: models.py from django.db import models from django.contrib.auth.models import AbstractUser # ...
- Spring BeanFactory 容器
Spring 的 BeanFactory 容器 这是一个最简单的容器,它主要的功能是为依赖注入 (DI) 提供支持,这个容器接口在 org.springframework.beans.factory. ...
- vue2.0+mint-ui资讯类顶导航和内容页联动实例(不是很完美)
<template> <div> <div class="navbox"> <div class="nav"> ...