Introduction

Who should read this article

This article is written for an audience of .NET programmers. Specifically, .NET programmers interested in using LINQ. More specifically yet, LINQ programmers wanting an introduction to implementing a LINQ data provider.

Searching for a LINQ Data Provider Example

I was looking for ways to develop a data provider using C#, and thought perhaps LINQ might provide one solution. While a few articles were published a few years ago (cerca 2010) about LINQ data providers, I could not find one that worked. That is, not one complete, working, project is available. I'd be pleased to be proved wrong.

I took one of the simplest half-finished projects and tweaked it to persuade it to work with Visual Studio 2013. The result is summarised here. You can download the project if you want to try it out.

LINQ

Usually, we use LINQ to query a data provider. Language-Integrated Query (LINQ) is great technology! It permits you to write queries against many kinds of data source, including XML and relational data. Its strength is that you write the query in your preferred language, such as C# or VB.NET. You don't have to write SQL to query a database, nor do you have to write XQuery to query XML data.

This article describes an unusual use of LINQ: to write a data provider. The goal behind that requirement is to fetch data from a non-relational source. The example project described here uses your computer's file system as a data source. Those data — files and folders — are available as a .NET IQueryable<> collection.

Microsoft .NET and Visual Studio

Microsoft .NET is both a software development environment and an operating system enhancement. If you are using Windows, then you probably have an installation of .NET on your computer. If you are a developer, then you already know about Microsoft Visual Studio and its .NET development capability.

LINQPad

LINQPad Prototype your ideas in LINQPad and then paste working code into Visual Studio. LINQPad is also a great way to learn LINQ! It is not just for LINQ queries, but any C#/F#/VB expression, statement block or program.

Simple LINQ Data Provider

While searching for examples of LINQ data providers, I found that many sample projects were incomplete, written in now-obsolete code, or required external resources that no longer exist. Eventually I came across this article by Jaco Pretorius. It describes an almost-working project. Jaco cites two Microsoft examples as inspiration …

The first project is rather complex, and did not fit my requirement for an introduction to the concepts of a LINQ data provider. The second project is simpler, but relies on the TerraServer-USA Web Services that no longer exist.

Unfortunately, Jaco's source code as published didn't work for me. Perhaps it was because I copied his code incorrectly, or perhaps it was because I'm using Visual Studio 2013 whereas his code was written for Visual Studio 2008.

I modified Jaco's code to create a complete, working, project. The Simple LINQ Data Provider makes a call to standard .NET methods to get a list of folders and files from a root directory that you specify in the main() function. It uses a set of System.Linq.Expressions classes to build an IQueryable list.

Once you have an IQueryable list you can use LINQ as a client to, well, query that list. You're free to use LINQ expressions to select a sub-set of results from the file system. For example, to quote from Jaco's article …

var query = from element in new FileSystemContext(@"C:\Downloads")
                 where element.ElementType == ElementType.File && element.Path.EndsWith(".zip")
                 orderby element.Path ascending
                 select element;

Enumerate that query to see what has been selected …

int i = 0;
foreach (var result in query)
{
    StringBuilder s = new StringBuilder();
    s.AppendFormat("Result {0} '{1}'", ++i, result.ToString());
    System.Console.WriteLine(s.ToString());
}

Assuming there are some ZIP files in your root folder, you should see a list of those files displayed in the Windows console.

Further Reading

Here's a non-exhaustive list of Internet articles about LINQ data providers. There are no printed materials that I have found about this topic (i.e. no books).

LA Solutions does not endorse or recommend these articles. They are listed purely as a summary of what I have found.

Bibliography
Title Author Comment
Implementing a custom LINQ provider Jaco Pretorius Visual Studio 2010
Building a LINQ Provider Pedram Rezaei
Creating an IQueryable LINQ Provider Microsoft References obsolete TerraServer-USA Web service
Building an IQueryable provider series Matt Warren Endorsed by Joseph Albahari (LINQPad)
LINQPad Data Context Extensibility Model Joseph Albahari

¶ Joseph Albahari

You may be familiar with the name Joseph Albahari. He is the co-author, with Ben Albahari, of O'Reilly book C# in a Nutshell. He is the author or co-author of several other books, and the driving force behind LINQPad.

C# in a Nutshell

Download

Download

You can download the Simple LINQ Data Provider Visual Studio project. It was developed using Visual Studio 2013; with other versions of Visual Studio your mileage may vary.


Return to .NET articles index.