最后调用的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(); }