Export (0) Print
Expand All

ObjectQuery<T>.Select Method

Limits the query results to only the properties that are defined in the specified projection.

Namespace:  System.Data.Objects
Assembly:  System.Data.Entity (in System.Data.Entity.dll)

public ObjectQuery<DbDataRecord> Select(
	string projection,
	params ObjectParameter[] parameters
)

Parameters

projection
Type: System.String

The list of selected properties that defines the projection.

parameters
Type: System.Data.Objects.ObjectParameter[]

Zero or more parameters that are used in this method.

Return Value

Type: System.Data.Objects.ObjectQuery<DbDataRecord>
A new ObjectQuery<T> instance of type DbDataRecord that is equivalent to the original instance with SELECT applied.

ExceptionCondition
ArgumentNullException

projection is null.

-or-

parameters is null.

ArgumentException

The projection is an empty string.

Select applies the projection specified by the projection parameter. The ObjectQuery<T> returned by the Select method is always a row type of DbDataRecord. Use the SelectValue<TResultType> method to return values that are simple types, entity types, or complex types. For more information, see Object Queries (Entity Framework).

When a navigation property is included in the projection, the query results include a collection of nested DbDataRecord objects. For more information, see How to: Navigate Relationships Using Navigation Properties (Entity Framework).

This example is based on the Adventure Works Sales Model. The example creates a new ObjectQuery<T> whose results are data records that contain the ProductID fields of the results of this query.

int productID = 900;
using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    string queryString = @"SELECT VALUE product FROM 
        AdventureWorksEntities.Products AS product
        WHERE product.ProductID > @productID";

    ObjectQuery<Product> productQuery1 =
        new ObjectQuery<Product>(queryString,
            context, MergeOption.NoTracking);

    productQuery1.Parameters.Add(new ObjectParameter("productID", productID));

    ObjectQuery<DbDataRecord> productQuery2 =
        productQuery1.Select("it.ProductID");

    foreach (DbDataRecord result in productQuery2)
    {
        Console.WriteLine("{0}", result["ProductID"]);
    }
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5 SP1

.NET Framework Client Profile

Supported in: 4

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft