Redigera

Dela via


ObjectContext.CreateQuery<T>(String, ObjectParameter[]) Method

Definition

Creates an ObjectQuery<T> in the current object context by using the specified query string.

public:
generic <typename T>
 System::Data::Objects::ObjectQuery<T> ^ CreateQuery(System::String ^ queryString, ... cli::array <System::Data::Objects::ObjectParameter ^> ^ parameters);
public System.Data.Objects.ObjectQuery<T> CreateQuery<T> (string queryString, params System.Data.Objects.ObjectParameter[] parameters);
member this.CreateQuery : string * System.Data.Objects.ObjectParameter[] -> System.Data.Objects.ObjectQuery<'T>
Public Function CreateQuery(Of T) (queryString As String, ParamArray parameters As ObjectParameter()) As ObjectQuery(Of T)

Type Parameters

T

The entity type of the returned ObjectQuery<T>.

Parameters

queryString
String

The query string to be executed.

parameters
ObjectParameter[]

Parameters to pass to the query.

Returns

An ObjectQuery<T> of the specified type.

Exceptions

The queryString or parameters parameter is null.

Examples

This example creates a simple query and iterates through the collection of results.

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    string queryString =
        @"SELECT VALUE contact FROM AdventureWorksEntities.Contacts
            AS contact WHERE contact.FirstName = @fn";

    ObjectQuery<Contact> contactQuery =
        context.CreateQuery<Contact>(queryString,
            new ObjectParameter("fn", "Frances"));

    // Iterate through the collection of Contact items.
    foreach (Contact result in contactQuery)
        Console.WriteLine("First Name: {0}, Last Name: {1}",
        result.FirstName, result.LastName);
}

Remarks

Use CreateQuery to create an ObjectQuery<T> of the specified type that belongs to the current object context.

Applies to

See also