今年6月初,刚参加工作,在公司做项目主要运用Linq 和WCF,刚开始获取数据列表和数据总记录数的时候还是用了两种方法,慢慢感觉这样效率不高。***想出一种好的方法,今天跟大家分享一下。
首先写一个Model
- /// <summary>
- /// 封装表中各个字段
- /// </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>
- ///将获得全部数据跟
- /// </summary>
- public class StudentList_Count
- {
- /// <summary>
- ///全部数据
- /// </summary>
- public List<Student> StudentList{ get; set; }
- /// <summary>
- ///数据的总条数
- /// </summary>
- public int StudentCount { get; set; }
- }
- /// <summary>
- ///分页参数
- /// </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,是实例化Linq类
- var _DB = new Dal.DB_StudentDataContext();
- var _Datas = from _Data in _DB.Student
- select _Data;
- StudentList_Count _DatasStuList= new StudentList_Count();
- //数据总记录数
- _DatasStuList.StudentCount = _Datas.Count();
- //分页数据
- _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