Export (0) Print
Expand All

Applying LINQ Queries to EntityDataSource

.NET Framework 4

This topic describes how to filter data in the EntityDataSource control during run time by using LINQ queries.

Starting with the .NET Framework version 4, the EntityDataSource control supports the QueryExtender control that is used to create filters for data that is retrieved from a data source. The query extender control can filter data in markup of a Web page by using declarative syntax. For more information, see Applying LINQ Queries to EntityDataSource. The EntityDataSource control also allows you to filter data by programmatically applying LINQ to Entities queries on top of the ObjectQuery<T> that is associated with the EntityDataSource by implementing the QueryCreated event handler. To programmatically filter data, you do not need to add the QueryExtender control to the page. The query extender control will be created when the[ E:System.Web.UI.WebControls.EntityDataSource.QueryCreated] event of the EntityDataSource control returns a different query from the one that was passed to the event.

You can set a LINQ query to specify additional filtering during run time on the EntityDataSource control, by implementing the EntityDataSource.QueryCreated event handler. The QueryCreated event occurs after the EntityDataSource is finished creating an Entity SQL query that was specified during the configuration of the EntityDataSource control. To apply the LINQ query on top of the original query provided by the EntityDataSource control, assign the LINQ query to the e.Query parameter that is passed to the event handler. The result type of the query that you assign to the e.Query parameter has to be the same type as the return type that is specified during the creation of the control, otherwise an exception will occur.

In the following example, a listbox is bound to the EntityDataSource control and the onquerycreated event is linked to the EntityDataSource1_QueryCreated event handler.

<asp:ListBox ID="ListBox1" runat="server" DataSourceID="EntityDataSource1" 
    DataTextField="SalesOrderID" DataValueField="SalesOrderID"></asp:ListBox>
<asp:EntityDataSource ID="EntityDataSource1" runat="server" 
    ConnectionString="name=AdventureWorksEntities" 
    DefaultContainerName="AdventureWorksEntities" EnableFlattening="False" 
    EntitySetName="SalesOrderHeaders" 
    onquerycreated="EntityDataSource1_QueryCreated">
</asp:EntityDataSource>

The event handler, implementing the QueryCreated event, adds a LINQ query to the EntityDataSource control that displays orders that have IDs less than 43661:

protected void EntityDataSource1_QueryCreated(object sender, QueryCreatedEventArgs e)
{
    var orders = e.Query.Cast<SalesOrderHeader>();
    e.Query = from order in orders
        where order.SalesOrderID < 43661
        select order;  
}

The EntityDataSource control will apply the sorting and paging expressions from the data-bound listbox control to the data after the Entity SQL and LINQ queries execute.

Ordering of the results is based on three different sources, which are applied in the following sequence:

  • The OrderBy or AutoGenerateOrderByClause properties of the EntityDataSource.

  • Ordering specified in a LINQ query or the QueryExtender during QueryCreated.

  • The ordering requested in the sort expression of a data-bound control.

Because each new ordering specification that you apply to a query overrides any previous ordering specification, only the last ordering specification in the sequence will affect the results, and any paging operations will be applied afterwards.

Community Additions

ADD
Show:
© 2014 Microsoft