BT

Kevin Halverson: How to implement IQueryable

| by Jonathan Allen Follow 636 Followers on Jul 23, 2007. Estimated reading time: 1 minute |

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

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss
BT