Data Projections (EntityDataSource)

You can project specific properties from the objects that an EntityDataSource control returns by using the Select property. The Select property of the EntityDataSource control contains a string that represents the SELECT statement of an Entity SQL query. This string is passed, without modification, to the ObjectQuery<T> that, when executed, returns the data to the EntityDataSource control. The string supplied to the Select property uses the same format as the string passed to the Select method of ObjectQuery<T>. For examples of how to use the SELECT clause to define a projection for a query, see How to: Execute a Query that Returns an Anonymous Type (Entity Framework).

Note

Updates are not supported for projected data. When you use the Select property to specify a projection, the data binding does not support updates.

The following XML markup uses the Select property to specify a projection with six of the properties of the Product type:

<asp:EntityDataSource ID="ProductDataSource" runat="server" 
    ConnectionString="name=AdventureWorksEntities" 
    DefaultContainerName="AdventureWorksEntities" 
    EntitySetName="Product" OrderBy="it.ProductID" 
    Select="it.ProductID, it.Name, it.ListPrice, 
    it.Size, it.Style, it.Weight">
</asp:EntityDataSource> 

The previous XML example is the same as the following ObjectQuery<T> named products:

ObjectQuery<DbDataRecord> products = context.Product
                    .Select("it.ProductID, it.Name, it.ListPrice, "
                    + "it.Size, it.Style, it.Weight").OrderBy("it.ProductID");

Passing Parameters

Like the Select method of the ObjectQuery<T> class, you can pass parameters to the projection that the Select property defines. You must define the SelectParameters property of the EntityDataSource control to specify a ParameterCollection for the SELECT statement of the query. The SelectParameters property uses named arguments to refer to the parameters in the Select property.

If you do not define the SelectParameters property, no parameter substitution is made. All the parameter names in the SELECT statement, prefixed by the "@" symbol, must have a matching name in the ParameterCollection. Null values are not allowed for parameters in a ParameterCollection.

The following example applies a 90% discount to each Product in the query by using a parameter on one of the property projections.

<asp:EntityDataSource ID="ProductDataSource2" runat="server" 
        ConnectionString="name=AdventureWorksEntities"
        DefaultContainerName="AdventureWorksEntities" 
        EntitySetName="Product" OrderBy="it.ProductID" 
        Select="it.ProductID, it.Name, it.ListPrice, 
            it.ListPrice * @Discount as DiscountPrice, 
            it.Size, it.Style, it.Weight"
        Where="it.ProductID > @ProductID">
        <SelectParameters>
            <asp:Parameter Name="Discount" DefaultValue=".90" Type="Decimal" />
        </SelectParameters>
        <WhereParameters>
            <asp:Parameter Name="ProductID" DefaultValue="700" Type="Int32" />
        </WhereParameters>
    </asp:EntityDataSource>

The previous XML example is the same as the following ObjectQuery<T> named products:

ObjectQuery<DbDataRecord> products = context.Product
                    .Select("it.ProductID, it.Name, it.ListPrice, " 
                        + "it.ListPrice * @Discount as DiscountPrice, "
                        + "it.Size, it.Style, it.Weight",
                    new ObjectParameter("Discount",.9M))
                    .Where("it.ProductID > @ProductID",
                    new ObjectParameter("ProductID", 700))
                    .OrderBy("it.ProductID");

See Also

Concepts

EntityDataSource Designer

Other Resources

Data Selection using EntityDataSource

Object Queries (Entity Framework)

Change History

Date

History

Reason

July 2008

Added topic.

SP1 feature change.