ObjectContext.CreateQuery(String, ObjectParameter[]) ObjectContext.CreateQuery(String, ObjectParameter[]) ObjectContext.CreateQuery(String, ObjectParameter[]) ObjectContext.CreateQuery(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 String String String

The query string to be executed.

parameters
ObjectParameter[]

Parameters to pass to the query.

Returns
Exceptions

Examples

This example is based on the AdventureWorks Sales Model.

The 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);
}
Using context As New AdventureWorksEntities()
    Dim queryString As String = "SELECT VALUE contact FROM AdventureWorksEntities.Contacts " & _
            " AS contact WHERE contact.FirstName = @fn"

    Dim contactQuery As ObjectQuery(Of Contact) = context.CreateQuery(Of Contact)(queryString, New ObjectParameter("fn", "Frances"))

    ' Iterate through the collection of Contact items. 
    For Each result As Contact In contactQuery
        Console.WriteLine("First Name: {0}, Last Name: {1}", result.FirstName, result.LastName)
    Next
End Using

Remarks

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

Applies to