Ordering Results (EntityDataSource)

You can order the results in your EntityDataSource control by using the OrderBy property. The OrderBy property of the EntityDataSource control is a string that represents an ORDER BY expression of an Entity SQL query. This string is passed, without modification, to the ObjectQuery<T> that serves as the data source of the EntityDataSource control. The string supplied to the OrderBy property uses the same format as the string passed to the OrderBy method of ObjectQuery<T>. For examples of how to use the ORDER BY clause to order query results, see How to: Sort Data (Entity Framework).

Passing Parameters

Like the OrderBy method of the ObjectQuery<T> class, you can pass parameters to the ORDER BY clause in the OrderBy property. You must define the OrderByParameters property of the EntityDataSource control to specify a ParameterCollection for the ORDER BY clause of the query. The OrderByParameters property uses a named argument to refer to the parameter assigned in the OrderBy property.

If you do not define the OrderByParameters property, no parameter substitution is made. The parameter name in the ORDER BY clause, prefixed by the "@" symbol, must have a matching name in the ParameterCollection. Null values are not allowed for parameters in a ParameterCollection.

Automatically Generating the ORDER BY Clause

You can automatically generate the ORDER BY clause by setting the AutoGenerateOrderByClause property of the EntityDataSource control to true. The EntityDataSource control will then automatically generate an ORDER BY clause from the parameter in the ParameterCollection that is assigned to the OrderByParameters property. This eliminates the need to explicitly assign an ORDER BY clause to the OrderBy property. When you automatically generate the ORDER BY clause from the OrderByParameters property, you must verify that the Name property of the parameter in the collection identifies a single property of the item returned from the query.

Example

The following example automatically generates the ORDER BY clause and uses the value of the orderByDropDownList to set the value of the parameter.

<asp:EntityDataSource ID="SalesOrderHeader" runat="server" 
    ConnectionString="name=AdventureWorksEntities"
    DefaultContainerName="AdventureWorksEntities" EnableDelete="True" 
    EnableInsert="True" EnableUpdate="True" EntitySetName="SalesOrderHeader" 
    Where="it.OnlineOrderFlag = true" AutoGenerateOrderByClause="True">
    <OrderByParameters>
        <asp:ControlParameter Name="OrderByParameter" 
            ControlID="orderByDropDownList" Type="String" />
    </OrderByParameters>
</asp:EntityDataSource>

See Also

Concepts

Configuring the EntityDataSource Control

Filtering Data (EntityDataSource)

EntityDataSource Designer

Configure Data Source Wizard (EntityDataSource Control)

Custom Command Text (EntityDataSource)

Other Resources

Querying Data as Objects (Entity Framework)

Change History

Date

History

Reason

July 2008

Added topic.

SP1 feature change.