加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何从IQueryable获取计数

发布时间:2020-12-15 17:47:15 所属栏目:百科 来源:网络整理
导读:我在GridView中实现分页.从 this文章,我需要两种方法: public IQueryable BindEmployees(int startRowIndex,int maximumRows){ EmployeeInfoDataContext dbEmp = new EmployeeInfoDataContext(); var query = from emp in dbEmp.Employees join dept in dbE
我在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);
}

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读