I am implementing paging in my GridView. From this article, I need two methods:
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);
}
And
public int GetEmployeeCount()
{
// How can I not repeat the logic above to get the count?
}
How can I get the value of the second method GetEmployeeCount
from the first method BindEmployees
? I mean without repeating the logic (the query)?
The IQueryable interface inherits the IEnumerable interface so that if it represents a query, the results of that query can be enumerated. Enumeration causes the expression tree associated with an IQueryable object to be executed.
GetList(context) might return an object backed by a custom LINQ provider (like an Entity Framework collection), then you probably want to leave the data cast as an IQueryable: even though your benchmark shows it being 20 times faster to use a list, that difference is so small that no user is ever going to be able to ...
IQueryable<T> is a C# interface that lets you query different data sources. The type T specifies the type of the data source that you're querying. Under the hood, IQueryable uses expression trees that translate LINQ queries into the query language for the data provided.
var list4 = list1. Union(list2); Union is a set operation - it returns distinct values. Concat simply returns the items from the first sequence followed by the items from the second sequence; the resulting sequence can include duplicate items.
One option would be:
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);
}
The other option is to pass the query into the paging function.
Make a function to use in both places:
//Can be private or public, your choice
private IQueryable<Employee> GetQuery()
{
EmployeeInfoDataContext dbEmp = new EmployeeInfoDataContext();
return from emp in dbEmp.Employees
join dept in dbEmp.Departments
on emp.DeptID equals dept.DeptID
select emp;
}
Then use it in both of your other functions:
public int GetEmployeeCount()
{
return GetQuery().Count();
}
public IQueryable BindEmployees(int startRowIndex, int maximumRows)
{
var query = from e in GetQuery()
select new { /*Do your anonymous type here*/ };
return query.Skip(startRowIndex).Take(maximumRows);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With