SIGN UP MEMBER LOGIN:    
ARTICLE

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

Posted by Anand Thakur Articles | ADO.NET in C# October 13, 2008
This article show us how to do conditional query and paging in ADO.NET Entity Framework.
Reader Level:

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.

Login to add your contents and source code to this article
share this article :
post comment
 

Hi Anand,


Nice Post, In my case I have to get the latest records, so I have to order them by descending.
Can you please post the query with orderby descending clause.

Regards,
Bala.

Posted by balu kumar Sep 09, 2010

This is a general question about the ADO.net framework. I have 2 tables Employee and EmployeeDetail. The 2 tables have a parent-child relation/association created when I built the .edmx file. I am able to populate a listbox with Employee(parent) information and return back the specific EmployeeDetail(child) information. However, I actually want to go the other way. I want to populate a list box with the EmployeeDetail information and return back the Employee information. My sample app is built with the Entity Framework QuickStart Guide, http://msdn.microsoft.com/en-us/library/bb399182.aspx. Thanks, Robert Harmon

Posted by Robert Nov 25, 2008
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Gauge for SharePoint
Become a Sponsor