我在GridView中实现分页.从
this文章,我需要两种方法:
public IQueryable BindEmployees(int startRowIndex,int maximumRows) { EmployeeInfoDataContext dbEmp = new EmployeeInfoDataContext(); var query = from emp in dbEmp.Employees join dept in dbEmp.Departments on emp.DeptID equals dept.DeptID select new { EmpID = emp.EmpID,EmpName = emp.EmpName,Age = emp.Age,Address = emp.Address,DeptName = dept.DepartmentName }; return query.Skip(startRowIndex).Take(maximumRows); }
和
public int GetEmployeeCount() { // How can I not repeat the logic above to get the count? }
如何从第一种方法BindEmployees中获取第二种方法GetEmployeeCount的值?我的意思是没有重复逻辑(查询)?
解决方法
一个选择是:
public IQueryable BindEmployees(int startRowIndex,int maximumRows,out int count) { EmployeeInfoDataContext dbEmp = new EmployeeInfoDataContext(); var query = from emp in dbEmp.Employees join dept in dbEmp.Departments on emp.DeptID equals dept.DeptID select new { EmpID = emp.EmpID,DeptName = dept.DepartmentName }; count = query.Count(); return query.Skip(startRowIndex).Take(maximumRows); }