LINQ Learning Guide: LINQ Providers

In this section of the LINQ Learning Guide, we link to tutorials that explain how to build LINQ providers and, thus, make a data source accessible through LINQ.




   LINQ Quick Start
   LINQ and Visual Studio 2008's new language features
   LINQ Providers
   LINQ and Web applications
   LINQ to Objects
   LINQ to XML
   LINQ to SQL
   LINQ to DataSet
   LINQ to Entities
   Third-party LINQ Implementations


LINQ Providers

Matt Warren, author of The Wayward Weblog and an architect on Microsoft's C# programming team, has put together a nice seven-part series on how to use IQueryable and IQueryProvider to build a LINQ provider, which is a way to make a data source accessible through LINQ.

Part I: Reusable IQueryable base classes
Here Warren offers an introduction to IQueryable, IQueryProvider, the CreateQuery method and the Execute method. "The real action happens inside the Execute method," he indicates. "That's where your provider has the opportunity to make sense of the query by examining the expression tree."

Part II: Where and reusable Expression tree visitor
In this installment Warren starts working on the sample provider. It's designed to translate a query into SQL command text and then translate the result of executing that command into objects. There's also an appendix that explains how to build an Expression Visitor.

Part III: Local variable references
Since the sample provider only understands one major query operator (and a few minor ones), and since the C# compiler makes a separate class for holding local variables, the LINQ provider needs to make sense of the trees that the compiler generates. Here Warren takes the Expression Visitor and uses it to write a rule for evaluating those trees.

Part IV: Select
The next step is to implement the Select method. In the LINQ Select operator, this can be done in many, many ways. "Fortunately, it's still easy. I just have to convert the selector function I already have into the one I need," Warren indicates. It involves getting the QueryTranslator to handle the Select method and return a lambda expression that is subsequently read by an ObjectReader.

Part V: Improved Column binding
This posts aims to fix the "gaping holes" in the LINQ expression trees, the QueryTranslator and the ColumnProjector that have been glossed over in Warren's head-first dive into his code.

Part VI: Nested queries
At this point Warren needs to finish off the Select method. He does so by executing a nested query. Watch what happens.

Part VII: Join and SelectMany
In this final post, Warren adds Join and SelectMany methods to his LINQ provider, seeing as, until now, it had only been executing Select and Where methods.


*** Go on to the next section of the LINQ Learning Guide: LINQ and Web applications

Dig Deeper on Visual Studio 2008 and Language Integrated Query (LINQ)

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.