PreCompiling LINQ Queries with Parameters

In my last post, I looked at precompiling a LINQ query to speed up (at least a little bit) your application. The LINQ statement I used as my example was very simple:

Dim res = From o In en.Orders
          Select o

This post looks at a more realistic example: a LINQ statement that has a Where clause. But I’ll also look at how to declare the variable holding the precompiled LINQ statement so that you can put your compiled statement to good use.

In my last post, I created a LINQ statement that had to be passed only the ObjectContext that it would use. In real life, most LINQ statements will require more than that to be passed to them. For instance, this LINQ statement retrieves only the orders for the customer number that’s specified in the variable called “custID”:

Dim res = From o In en.Orders
          Where o.CustomerID = custId
          Select o

Effectively, that custId variable is a parameter that must be passed to the pre-compiled LINQ statement when the statement is executed.

If your statement does require parameters then, when compiling it, you must specify the parameter in two places:

  • In the data type list used by the Compile method: You must specify the data type of the parameter
  • In the parameter list for the lambda function: You must specify the name that will be used in the LINQ query to refer to the parameter

Enhancing the Compile method example from my last post to precompile my Where-clause query to accept the custId string parameter gives this code:

Dim qry = System.Data.Objects.CompiledQuery.Compile(
                    Of northwndModel.northwndEntities,
                    String,
                    IQueryable(Of northwndModel.Order)) _
(Function(en, custid) From o In en.Orders
                                Where o.CustomerID = custid
                                Select o)

Calling the LINQ statement and passing both parameters (the ObjectContext and a customer Id) would look like this:

Dim oc As New northwndModel.northwndEntities
Dim res = qry.Invoke(oc, “ALFKI”)

For this technique to be useful, though, you’ll want to set the qry variable to the precompiled query at the start of your application and then invoke the query some other method (or in several other methods). As a result, you’ll probably want to declare the variable holding the compiled query outside of any method. You must declare the variable using the Func type—another generic type that must be told what data types it’s working with. Fortunately, you can just copy declaration you passed to the Compile method to define the qry variable.

For the last example with the string parameter, the declaration of the qry variable would look like this:

Dim qry As Func(Of northwndModel.northwndEntities,
                String,
                IQueryable(Of northwndModel.Order))

Now, at the start of your application you can compile your LINQ statements into a set of variables and, later in your application, use the variable’s Inovke method to execute the statement.

Peter Vogel

Type to search blog.learningtree.com

Do you mean "" ?

Sorry, no results were found for your query.

Please check your spelling and try your search again.