Getting started with ADO.NET Entity Framework in .NET 3.5 - Part III

Now let us extend the last tutorial for querying and paging.

Now in the example given in last tutorial update the GetEmployee method as follows

public static List<Employee> GetEmployee()
{
int skipValue = 50;
int limitValue = 25;
PayrollDAO payrollDAO = new PayrollDAO ();
ObjectQuery<Employee> employeeQuery = payrollDAO.Employee
.Where("it.Name = @eName", new
ObjectParameter ("eName", "aaa"))
.Where("it.EmployeeID > @EmpID1 ", new
ObjectParameter("EmpID1", 20083))
.Where("it.EmployeeID < @EmpID2 ", new
ObjectParameter("EmpID2", 20099))
.Skip("it.Name", "@skip", new
ObjectParameter("skip", skipValue))
.Top("@limit", new ObjectParameter("limit", limitValue));
return employeeQuery.ToList();
}

Conditional Query

As you can see from the code WHERE condition

.Where("it.Name = @eName", new ObjectParameter("eName", "aaa"))

Select the name whose value equal to eName parameter and next is the parameter passed in case it is 'aaa'. Please note that "it" is the default name given by Entity Framework. This will select all the employees with name 'aaa'.

You can add as much as WHERE conditions as you can see in the above example.

More information http://msdn.microsoft.com/en-us/library/bb896238.aspx

Paging

Paging in Entity framework is done by SKIP and TOP.

SKIP tells that from where the data should be picked up and TOP tells how many rows to be selected

int skipValue = 50;
int limitValue = 25;
.Skip("it.Name", "@skip", new
ObjectParameter("skip", skipValue))
.Top("@limit", new
ObjectParameter("limit", limitValue));

In this example it will skip first 50 rows, then starting from 51st row; it will show up to 75th row .You can also explore LIMIT method for paging


Aah! Another bug ! Well , it's the life.


Similar Articles