Filtering Data (EntityDataSource)

The Where property of the EntityDataSource control is a string that represents a WHERE clause that is the predicate of an Entity SQL query. This string is passed, without modification, to an ObjectQuery<T> that is executed by Object Services. This query is the source of the data regulated by the EntityDataSource control. The string supplied to the Where property uses the same format as the string passed to the Where method of ObjectQuery<T>. For examples of how to use the WHERE clause to filter a query, see How to: Filter Data (Entity Framework).

Passing Parameters

Like the Where method of the ObjectQuery<T> class, parameters can be passed to the predicate assigned to the Where property. The WhereParameters property of the EntityDataSource control specifies a ParameterCollection that contains the parameters to supply to the WHERE clause of the query. The WhereParameters property uses named arguments to refer to the parameters specified in the string supplied to the Where property.

If the WhereParameters property is not set, no parameter substitution is made. All of the parameter names in the WHERE clause, prefixed by the "@" symbol, must have a matching name in the ParameterCollection. Null values are not allowed for parameters in a ParameterCollection.

Example

The XML markup in the following example, in an .aspx file, retrieves a value from a control and passes it as a parameter to the Where property.

<asp:EntityDataSource ID="SalesOrderHeader" runat="server" 
  ConnectionString="name=AdventureWorksEntities" 
  DefaultContainerName="AdventureWorksEntities" EnableDelete="True" 
  EnableInsert="True" EnableUpdate="True" EntitySetName="SalesOrderHeader" 
  EntityTypeFilter="" OrderBy="it.TotalDue DESC" Select="" 
   Where="it.OnlineOrderFlag = TRUE AND it.TotalDue &gt; @ordercost">
  <WhereParameters>
    <asp:ControlParameter ControlID="costLimit" DbType="Int32" 
      DefaultValue="2500" Name="ordercost" PropertyName="Text" />
  </WhereParameters>
</asp:EntityDataSource>

The previous XML example is equivalent to the following ObjectQuery<T>, named onlineOrders:

ObjectQuery<SalesOrderHeader> onlineOrders =
      context.SalesOrderHeader
       .Where("it.OnlineOrderFlag = TRUE AND it.TotalDue > @ordercost",
         new ObjectParameter("ordercost", orderCost))
        .OrderBy("it.TotalDue DESC");

Automatically Generating the WHERE Clause

When the AutoGenerateWhereClause property of the EntityDataSource control is set to true, the control automatically generates a WHERE clause from the parameters in the ParameterCollection assigned to the WhereParameters property. This eliminates the need to explicitly assign a WHERE clause to the Where property. The construction of the WHERE clause from the WhereParameters property requires that the Name property of each parameter in the collection identifies a single property of the item returned from the query.

Example

The following example assumes that a checkbox sets the online order flag:

<asp:EntityDataSource ID="SalesOrderHeader" runat="server" 
    ConnectionString="name=AdventureWorksEntities" 
    DefaultContainerName="AdventureWorksEntities" EnableDelete="True" 
    EnableInsert="True" EnableUpdate="True" EntitySetName="SalesOrderHeader" 
    EntityTypeFilter="" Select="" AutoGenerateWhereClause="True">
    <WhereParameters>
        <asp:ControlParameter ControlID="onlineOrder" DefaultValue="true" 
            Name="OnlineOrderFlag" PropertyName="Text" DbType="Boolean" />
    </WhereParameters>
</asp:EntityDataSource>

See Also

Concepts

Configuring the EntityDataSource Control

EntityDataSource Designer

Other Resources

Object Services Overview (Entity Framework)

Query Builder Methods (Entity Framework)

Change History

Date

History

Reason

July 2008

Added topic.

SP1 feature change.