This documentation is archived and is not being maintained.

CompiledQuery.Compile Method

Creates a new delegate that represents the compiled query.

This member is overloaded. For complete information about this member, including syntax, usage, and examples, click a name in the overload list.

  NameDescription
Public method Static member Compile(Of TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TResult)(Expression(Of Func(Of TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TResult))) Compiles the query.
Public method Static member Compile(Of TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TResult)(Expression(Of Func(Of TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TResult))) Compiles the query.
Public method Static member Compile(Of TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TResult)(Expression(Of Func(Of TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TResult))) Compiles the query.
Public method Static member Compile(Of TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TResult)(Expression(Of Func(Of TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TResult))) Compiles the query.
Public method Static member Compile(Of TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TResult)(Expression(Of Func(Of TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TResult))) Compiles the query.
Public method Static member Compile(Of TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TArg13, TResult)(Expression(Of Func(Of TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TArg13, TResult))) Compiles the query.
Public method Static member Compile(Of TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TArg13, TArg14, TResult)(Expression(Of Func(Of TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TArg13, TArg14, TResult))) Compiles the query.
Public method Static member Compile(Of TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TArg13, TArg14, TArg15, TResult)(Expression(Of Func(Of TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TArg13, TArg14, TArg15, TResult))) Compiles the query.
Public method Static member Compile(Of TArg0, TResult)(Expression(Of Func(Of TArg0, TResult))) Compiles the query.
Public method Static member Compile(Of TArg0, TArg1, TResult)(Expression(Of Func(Of TArg0, TArg1, TResult))) Compiles the query.
Public method Static member Compile(Of TArg0, TArg1, TArg2, TResult)(Expression(Of Func(Of TArg0, TArg1, TArg2, TResult))) Compiles the query.
Public method Static member Compile(Of TArg0, TArg1, TArg2, TArg3, TResult)(Expression(Of Func(Of TArg0, TArg1, TArg2, TArg3, TResult))) Compiles the query.
Public method Static member Compile(Of TArg0, TArg1, TArg2, TArg3, TArg4, TResult)(Expression(Of Func(Of TArg0, TArg1, TArg2, TArg3, TArg4, TResult))) Compiles the query.
Public method Static member Compile(Of TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TResult)(Expression(Of Func(Of TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TResult))) Compiles the query.
Public method Static member Compile(Of TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TResult)(Expression(Of Func(Of TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TResult))) Compiles the query.
Public method Static member Compile(Of TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TResult)(Expression(Of Func(Of TArg0, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TResult))) Compiles the query.
Top

The generic arguments to this method are (in order):

  • TDataContext. Constrained to be a DataContext. It represents the DataContext that must be passed in when executing the delegate returned by the Compile method.

  • TArg0, …, TArg2. Represents the types of the parameters that have to be passed in when executing the delegate returned by the Compile method.

  • TResult. Represents the type of T in the IEnumerable<T> returned when executing the delegate returned by the Compile method.

Passing a query expression that does not use the TDataContext parameter generates an exception when the returning delegate is executed for the first time.

The Return Value

Func<TDataContext, TArg0, TArg1, …, TArg2, TResult>, representing the delegate that can be cached. At the time when this delegate is executed, the query will be compiled. Observe the following notes:

  • Passing null to the first argument typed TDataContext generates a run-time exception.

  • It is an error to re-execute a compiled query using a DataContext that contains a DataLoadOptions different from the one originally used to compile the query (the first time it is executed). The reason for this behavior is that compiling the query requires information that is contained in the DataShape. When a query is executed again using a different DataShape, the cached query becomes invalid and an error is generated.

  • If a new query operator is applied to the result of the delegate execution, an exception is generated.

  • When you want to execute a query operator over the result of executing a compiled query, you must translate the result to a list before operating on it.

The following code example assumes a Queries class designed to store compiled queries, and assumes a Northwind class that represents a strongly typed DataContext.


Class Queries

    Public Shared CustomersByCity As  _
        Func(Of Northwnd, String, IQueryable(Of Customer)) = _
            CompiledQuery.Compile(Function(db As Northwnd, _
    city As String) _
        From c In db.Customers Where c.City = city Select c)

    Public Shared CustomersById As  _
        Func(Of Northwnd, String, IQueryable(Of Customer)) = _
            CompiledQuery.Compile(Function(db As Northwnd, _
    id As String) _
        db.Customers.Where(Function(c) c.CustomerID = id))

End Class



' The following example invokes such a compiled query in the main
' program
Public Function GetCustomersByCity(ByVal city As String) As  _
    IEnumerable(Of Customer)

    Dim myDb = GetNorthwind()
    Return Queries.CustomersByCity(myDb, city)
End Function


You cannot currently store (in static variables) queries that return an anonymous type, because the type has no name to provide as a generic argument. The following example shows how you can work around the issue by creating a type that can represent the result, and then use it as a generic argument.


Class SimpleCustomer
    Private _ContactName As String
    Public Property ContactName() As String
        Get
            Return _ContactName
        End Get
        Set(ByVal value As String)
            _ContactName = value
        End Set
    End Property
End Class

Class Queries2
    Public Shared CustomersByCity As Func(Of Northwnd, String, IEnumerable(Of SimpleCustomer)) = _
        CompiledQuery.Compile(Of Northwnd, String, IEnumerable(Of SimpleCustomer))( _
        Function(db As Northwnd, city As String) _
        From c In db.Customers _
        Where c.City = city _
        Select New SimpleCustomer With {.ContactName = c.ContactName})
End Class


Show: