ObjectQuery<T>.SelectValue<TResultType> Method

Definition

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

C#
public System.Data.Objects.ObjectQuery<TResultType> SelectValue<TResultType>(string projection, params System.Data.Objects.ObjectParameter[] parameters);

Type Parameters

TResultType

The type of the ObjectQuery<T> returned by the SelectValue<TResultType>(String, ObjectParameter[]) method.

Parameters

projection
String

The projection list.

parameters
ObjectParameter[]

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

Returns

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.

Exceptions

projection is null.

-or-

parameters is null.

The projection is an empty string.

Examples

This example creates a new ObjectQuery<T> that contains results that are a sequence of ProductID values projected from the results of the existing query.

C#
using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    string queryString =
        @"SELECT VALUE product FROM
            AdventureWorksEntities.Products AS product";

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

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

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

Remarks

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

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

Applies to

Product Versions
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

See also