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.htmlhtml

DataTable分頁代碼http://www.cnblogs.com/jyshi/archive/2011/08/09/2132758.html

1.Math.Ceiling() web

返回大於或等於指定的雙精度浮點數的最小整數值。post

例如:Math.Ceiling(32.6)=33; Math.Ceiling(32.0)=32;url

 

 

2.Math.Floor() spa

用法和ceiling相反,返回小於或等於指定的雙精度浮點數的最小整數值。orm

例如: Math.Floor(32.6)=32;;Math.Floor(32.0)==32;htm

 

3.Math.Round()blog

取指定位數的小數。ip

例如: Math.Round(36.236,2)=36.24; Math.Round(36.232,2)=36.23;ci

 

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();
                        }
相關文章
相關標籤/搜索