Export (0) Print
Expand All

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).

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");

If you want to compare an entity type property and expression for equality, you can use the AutoGenerateWhereClause property. 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 of the WhereParameters property. The Name property of each parameter has to match an entity type property name that is included in the query results. If you set the AutoGenerateWhereClause property to true, you should not explicitly assign a WHERE clause to the Where property.

Example

In the following example, the AutoGenerateWhereClause property is set to true. Therefore, the Name of the property has to match an entity type property name that is included in the query results. The query selects the SalesOrderID and TotalDue properties of the SalesOrderHeader entity type. The automatically generated WHERE clause will filter the query results by comparing the TotalDue value to a value selected in the TotalDueList list box control.

<asp:EntityDataSource ID="SalesOrderHeaderWithAutoParam" runat="server" 
   ConnectionString="name=AdventureWorksEntities" 
   DefaultContainerName="AdventureWorksEntities" 
   EntitySetName="SalesOrderHeaders" 
   Select="it.SalesOrderID, it.TotalDue"
    AutoGenerateWhereClause="True">
    <WhereParameters>
        <asp:ControlParameter ControlID="TotalDueList" 
           Name="TotalDue" PropertyName="SelectedValue" DbType="Decimal" />
    </WhereParameters>
</asp:EntityDataSource>

Example

By default, the AutoGenerateWhereClause property is set to false. This means that we have to supply the WHERE clause. In the following example, the WHERE clause is Where="it.TotalDue < @totalDueParam" and totalDueParam is the parameter name.

<asp:EntityDataSource ID="SalesOrderHeaderWithOutAutoParam" runat="server" 
   ConnectionString="name=AdventureWorksEntities" 
   DefaultContainerName="AdventureWorksEntities" 
   EntitySetName="SalesOrderHeaders" 
   Where="it.TotalDue < @totalDueParam" 
    Select="it.SalesOrderID, it.TotalDue">
    <WhereParameters>
        <asp:ControlParameter ControlID="TotalDueList" 
           Name="totalDueParam" PropertyName="SelectedValue" DbType="Decimal" />
    </WhereParameters>
</asp:EntityDataSource>

Community Additions

ADD
Show:
© 2014 Microsoft