Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Kevin Halverson: How to implement IQueryable

Kevin Halverson: How to implement IQueryable

In a two-part series, Kevin Halverson has demonstrated how to create a LINQ provider by implementing the IQueryable and IQueryProvider interfaces. Specifically he uses the Windows Desktop Search as a data source.

He starts by explaining how the CreateQuery method works. In his example, Kevin translates the abstract syntax tree into a SQL statement suitable for passing to Windows Desktop Search.

An abstract syntax tree is an expression represented as a collection of objects. Usually used as an intermediate step between raw source code and compiled code, it can also be used at runtime to allow an application to examine itself.

Another concept introduced by Kevin is closures. Closures, and the related concept lambda expressions, are used throughout the LINQ query design.

Well, the Linq architecture revolves around the concept of delayed execution. In other words, I create the query at one point, but I don’t actually evaluate it (capture input values and query underlying data source) until I start to use the query results. Because of this, we want to capture the information about how to access the contents of cutoffDate, but we don’t want to store the value away just yet. What I’m doing is placing a token ([value*]) in the query string and then creating a function that I can use to get the value of cutoffDate when the results of the query are accessed.

An interesting side-effect of abstract syntax trees is that you can completely replace one function call with another. In this example, the function LikeString is translated into the SQL Like operator.

In Part 2, Kevin completes the walk-through by discussing the GetEnumerator function. Here other techniques such as compiling an abstract syntax tree and resolving variables in closures are discussed.

Rate this Article