Читати англійською Редагувати

Поділитися через


ObjectQuery<T>.Select(String, ObjectParameter[]) Method

Definition

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

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

Parameters

projection
String

The list of selected properties that defines the projection.

parameters
ObjectParameter[]

Zero or more parameters that are used in this method.

Returns

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

Exceptions

projection is null.

-or-

parameters is null.

The projection is an empty string.

Examples

This example creates a new ObjectQuery<T> whose results are data records that contain the ProductID fields of the results of this query.

C#
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"]);
    }
}

Remarks

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 method to return values that are simple types, entity types, or complex types. For more information, see LINQ to Entities.

When a navigation property is included in the projection, the query results include a collection of nested DbDataRecord objects.

Applies to

Продукт Версії
.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