談Linq中獲取數(shù)據(jù)分頁高效方法
作者:JesseWong
今年6月初,剛參加工作,在公司做項(xiàng)目主要運(yùn)用Linq 和WCF,剛開始獲取數(shù)據(jù)列表和數(shù)據(jù)總記錄數(shù)的時(shí)候還是用了兩種方法,慢慢感覺這樣效率不高。最后想出一種好的方法,今天跟大家分享一下。
今年6月初,剛參加工作,在公司做項(xiàng)目主要運(yùn)用Linq 和WCF,剛開始獲取數(shù)據(jù)列表和數(shù)據(jù)總記錄數(shù)的時(shí)候還是用了兩種方法,慢慢感覺這樣效率不高。***想出一種好的方法,今天跟大家分享一下。
首先寫一個(gè)Model
- /// <summary>
- /// 封裝表中各個(gè)字段
- /// </summary>
- public class Student
- {
- public Guid S_ID { get; set; }
- public string SJD_MC { get; set; }
- public DateTime S_Time { get; set; }
- public string S_BZ { get; set; }
- }
- /// <summary>
- ///將獲得全部數(shù)據(jù)跟
- /// </summary>
- public class StudentList_Count
- {
- /// <summary>
- ///全部數(shù)據(jù)
- /// </summary>
- public List<Student> StudentList{ get; set; }
- /// <summary>
- ///數(shù)據(jù)的總條數(shù)
- /// </summary>
- public int StudentCount { get; set; }
- }
- /// <summary>
- ///分頁參數(shù)
- /// </summary>
- public class Student_FY : Student
- {
- public int PageIndex { get; set; }
- public int PageSize { get; set; }
- }
然后
- public StudentList_Count StudentList(int pageIndex,int pageSize)
- {
- //_DB,是實(shí)例化Linq類
- var _DB = new Dal.DB_StudentDataContext();
- var _Datas = from _Data in _DB.Student
- select _Data;
- StudentList_Count _DatasStuList= new StudentList_Count();
- //數(shù)據(jù)總記錄數(shù)
- _DatasStuList.StudentCount = _Datas.Count();
- //分頁數(shù)據(jù)
- _DatasStuList.StudentList = _Datas.Count() > 0 ? _Datas.Skip(pageIndex - 1) *pageSize).Take(pageSize).ToList() : null;
- return _DatasStuList;
- }
原文鏈接:http://www.cnblogs.com/jessewong/archive/2012/09/18/2690049.html
責(zé)任編輯:林師授
來源:
博客園