Export (0) Print
Expand All

ObjectQuery<T>.SelectValue<TResultType> Method

Limits the query results to only the property specified in the projection.

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

public ObjectQuery<TResultType> SelectValue<TResultType>(
	string projection,
	params ObjectParameter[] parameters
)

Type Parameters

TResultType

The type of the ObjectQuery<T> returned by the SelectValue<TResultType> method.

Parameters

projection
Type: System.String

The projection list.

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

An optional set of query parameters that should be in scope when parsing.

Return Value

Type: System.Data.Objects.ObjectQuery<TResultType>
A new ObjectQuery<T> instance of a type compatible with the specific projection. The returned ObjectQuery<T> is equivalent to the original instance with SELECT VALUE applied.

ExceptionCondition
ArgumentNullException

projection is null.

-or-

parameters is null.

ArgumentException

The projection is an empty string.

SelectValue<TResultType> is used to return values that are simple types, entity types, or complex types. Use the Select method for projections that require a row type instead of a value type. For more information, see Object Queries (Entity Framework).

The SelectValue<TResultType> method applies the projection specified by the projection parameter. The ObjectQuery<T> returned by the SelectValue<TResultType> method must be of a value type that is compatible with the projection and must be the same type as the type of SelectValue<TResultType>.

This example is based on the Adventure Works Sales Model. The example creates a new ObjectQuery<T> that contains results that are a sequence of ProductID values projected from the results of the existing query.

using (AdventureWorksEntities advWorksContext =
    new AdventureWorksEntities())
{
    try
    {
        string queryString =
            @"SELECT VALUE Product FROM 
                AdventureWorksEntities.Product AS Product";

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

        ObjectQuery<Int32> productQuery2 = 
            productQuery1.SelectValue<Int32>("it.ProductID");

        foreach (Int32 result in productQuery2)
        {
            Console.WriteLine("{0}", result);
        }
    }
    catch (EntitySqlException ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

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

.NET Framework

Supported in: 3.5 SP1

Community Additions

ADD
Show:
© 2015 Microsoft