c# – 如何从IQueryable获取计数

前端之家收集整理的这篇文章主要介绍了c# – 如何从IQueryable获取计数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在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);
}

另一个选项是将查询传递到寻呼功能.

原文链接:https://www.f2er.com/csharp/96950.html

猜你在找的C#相关文章