我正在我的 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,
EmpName = emp.EmpName,
Age = emp.Age,
Address = emp.Address,
DeptName = dept.DepartmentName
};
count = query.Count();
return query.Skip(startRowIndex).Take(maximumRows);
}
另一种选择是将查询传递给分页函数。
关于c# - 如何从 IQueryable 获取计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5449863/