This documentation is archived and is not being maintained.

ObjectContext.CreateQuery(Of T) Method

Creates an ObjectQuery(Of T) in the current object context by using the specified query string.

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

'Declaration
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(Of T).

Parameters

queryString
Type: System.String
The query string to be executed.
parameters
Type: System.Data.Objects.ObjectParameter()
Parameters to pass to the query.

Return Value

Type: System.Data.Objects.ObjectQuery(Of T)
An ObjectQuery(Of T) of the specified type.

ExceptionCondition
ArgumentNullException

The queryString or parameters parameter is Nothing.

Use CreateQuery(Of T) to create an ObjectQuery(Of T) of the specified type that belongs to the current object context.

This example is based on the Adventure Works Sales Model.

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


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


.NET Framework

Supported in: 4, 3.5 SP1

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Show: