Linq to Object实现分页获取数据
最后调用的CSharp代码可以是:
int totalcount = dboperator.Customers.Count(); //总记录数
int pagesize = 100;//每页显示数
//错误写法
int pages = (int)Math.Round((double)totalcount / pagesize);//总页数
//正确写法:向上取整
int pages= (int)Math.Ceiling((double)totalcount / pagesize);
for (int pageindex = 1; pageindex <= pages; pageindex++)
{
var customers = dboperator.Customers.OrderBy(c => c.ContactName)
.Skip((pageindex - 1) * pageSize)
.Take(pageSize);
//...
}
加入总记录数为501,每页显示100,上面pages总页数为5。正确应该为6
修改:
//int pages = (int)Math.Round((double)totalcount / pagesize);
int pages = PageCount(totalcount, pagesize);
/// <summary>
/// 返回分页的页数
/// </summary>
/// <param name="count">总条数</param>
/// <param name="pageye">每页显示多少条</param>
/// <returns>如果 结尾为0:则返回1</returns>
public static int PageCount(int count, int pageye)
{
int page = 0;
int sesepage = pageye;
if (count % sesepage == 0) { page = count / sesepage; }
else { page = (count / sesepage) + 1; }
if (page == 0) { page += 1; }
return page;
}
本文参考博客:http://www.cnblogs.com/wintersun/archive/2009/10/06/1578401.html
DataTable分页代码:http://www.cnblogs.com/jyshi/archive/2011/08/09/2132758.html
1.Math.Ceiling()
返回大于或等于指定的双精度浮点数的最小整数值。
例如:Math.Ceiling(32.6)=33; Math.Ceiling(32.0)=32;
2.Math.Floor()
用法和ceiling相反,返回小于或等于指定的双精度浮点数的最小整数值。
例如: Math.Floor(32.6)=32;;Math.Floor(32.0)==32;
3.Math.Round()
取指定位数的小数。
例如: Math.Round(36.236,2)=36.24; Math.Round(36.232,2)=36.23;
4.Math.Log()
取指定数字在使用指定底时的对数。
例如: 一本16开的书,计算对开了几次。Math.Log(16,2)=4
Linq 对List<T>进行分页:
int pageSize = 150;
int totalPageCount = 0;
totalPageCount = (gasBottlesList.Count() / pageSize + 1); for (int i = 1; i <= totalPageCount; i++)
{
string msg = string.Empty;
msg = "执行进度(" + i.ToString2() + "/" + totalPageCount.ToString2() + ")";
frmLoading.SetCaption(msg);
status = DataCenterService.Instance.ImportGasBottlesBySqlBulkCopy(gasBottlesList.Take(pageSize * i).Skip(pageSize * (i - 1)).ToArray());
frmLoading.SetCaption(msg + ",结果:" + (status == true ? "成功" : "失败"));
//System.Threading.Thread.Sleep(300);
Application.DoEvents();
}
Linq to Object实现分页获取数据的更多相关文章
- SQL Server 怎么在分页获取数据的同时获取到总记录数
SQL Server 获取数据的总记录数,有两种方式: 1.先分页获取数据,然后再查询一遍数据库获取到总数量 2.使用count(1) over()获取总记录数量 SELECT * FROM ( SE ...
- redis分页获取数据
php代码: 采用哈希类型存储数据,有序集合存储分页数据,进行倒序与正序的排序. $getGoodsInfo = M('goods_test')->select(); for($i=0;$i&l ...
- tp5 使用paginate分页获取数据对象之后 如何对对象进行数据添加
tp5 使用paginate分页获取数据对象之后 如何对对象进行数据添加 大家都知道,在使用tp5的paginate获取分页数据之后,得到的是一个数据对象,但有时会碰到要对数据对象进行二次加工的情况, ...
- 腾讯云图片鉴黄集成到C# SQL Server 怎么在分页获取数据的同时获取到总记录数 sqlserver 操作数据表语句模板 .NET MVC后台发送post请求 百度api查询多个地址的经纬度的问题 try{}里有一个 return 语句,那么紧跟在这个 try 后的 finally {}里的 code 会 不会被执行,什么时候被执行,在 return 前还是后? js获取某个日期
腾讯云图片鉴黄集成到C# 官方文档:https://cloud.tencent.com/document/product/641/12422 请求官方API及签名的生成代码如下: public c ...
- 【Django+Element UI】使用一个接口文件,搞定分页获取数据,模糊查询后分页获取数据
1:序列化获取数据的接口设计 1:分页获取序列化数据 2:是个能传参数的接口 class Society(APIView): def post(self, request): keywords = s ...
- NHibernte 4.0.3版本中,使用Queryover().Where().OrderBy().Skip().Take()方法分页获取数据失败
问题代码如下: var result=repository.QueryOver<modal>() .Where(p=>p.Code==Code) .OrderBy(p=>p.I ...
- SQL分页获取数据
SQL Server分页 select * from (') t Oracle分页 SELECT * FROM (' ORDER BY MaterialNM) t
- linq查询集合并分页展示数据
private void Bind() { if (Request.QueryString["QuestionNo"] != null) { string QuestionNo = ...
- MVC 分页获取数据 及点选按钮
@model PagedList<Lyxm.Entity.Suggestion>@using Webdiyer.WebControls.Mvc <div> <ul ...
随机推荐
- MySQL设置
在MySQL的使用中很容易出现乱码的情况. 实际上在MySQL中有个地方表明了系统中所用到的所有的字符集. 例如: 从中可以看出,对于server和database的默认字符集都是latin1,这样很 ...
- io开发之C语言第二天
开发环境是OS X系统下的Xcode Xcode的两个快捷键以及打开Xcode项目的正确方式 快捷键:command + B 编译 + 链接 快捷键:command + R 编译 + 链接 + 运行 ...
- ByteArrayInputStream 和 ByteArrayOutputStream
package java.io; /** * A <code>ByteArrayInputStream</code> contains * an internal buffer ...
- 4d tensor
偶然在一个ppt中看到了如下关于tensor的解释,清晰明白,所以post在这里,以备后续查看 根据这个理解: theano中的input(4d tensor):[mini-batch size, n ...
- [BZOJ 3585] mex 【莫队+分块】
题目链接:BZOJ - 3585 题目分析 区间mex,即区间中没有出现的最小自然数. 那么我们使用一种莫队+分块的做法,使用莫队维护当前区间的每个数字的出现次数. 然后求mex用分块,将权值分块(显 ...
- ping命令找不到
重装系统后安装JDK了,网络一直不好,我ping了下,结果显示ping不是内部或者外部命令,在谷歌里百度了下,在环境变量的path后加上“;C:\Windows\System32”即可,果然有效哦. ...
- Js 表单序列化
http://www.w3cmm.com/javascript/serialize-form.html
- 14.8.3 Physical Row Structure of InnoDB Tables InnoDB 表的物理行结构
14.8.3 Physical Row Structure of InnoDB Tables InnoDB 表的物理行结构 一个InnoDB 表的物理行结构取决于在创建表指定的行格式 默认, Inno ...
- 【HDOJ】2386 Dart Challenge
纯粹母函数+滚动数组,水之. /* 2386 */ #include <iostream> #include <string> #include <map> #in ...
- poj1849
不难发现每条边最多走两次,最少走一次也就是我们要在所有走两次的边中选两条从根出发没有公共边的路径使路径上的边少走一次显然我们找的是最长路径