Over the holidays Alex Turner, Mary Deyo and I added a new sample to the downloadable version of the CSharp samples that accompany Visual Studio 2008. This sample is called the LinqToTerraServerProvider, and it appears in a sub-directory called WebServiceLinqProvider. The LINQ provider technology that makes this sample possible is also used by other tools such as LINQ to Amazon, LINQ to LDAP, LINQ to SharePoint, and LINQ to Google Desktop.

When you download the samples, unzip the package into your Documents or My Documents directory or some other location where you have full rights. Navigate to the LinqSamples directory and then the directory called WebServiceLinqProvider. Open the LinqToTerraServerProvider solution. If you get a warning stating the "Project location is not trusted" you can safely ignore it. To avoid getting the warning in the future, right click on the solution file and choose properties. Select the Unblock button in the security section.


The TerraServer example shows how to create a LINQ provider. The provider technology makes it possible for your to extend LINQ to enable querying a new data source. For instance, LINQ ships with the ability to query SQL servers, XML files and generic objects. The LINQ provider technology allows you to extend LINQ so you can query a new data source such as a text file or a web site. This particular sample shows how to write a provider that makes it possible for LINQ developers to query the web service provided by the TerraServer mapping and aerial photography web site.

The following query from the sample will return a list of states in the US that have a city, town or some other location called Redmond:

var query1 = from place in terraPlaces
             where place.Name== "Redmond"
             select place.State;

Here is the data returned by this query:

West Virginia

The query shown below requests a list of the places, ordered by state, that have a place called either Johannesburg, Yachats, or Seattle:

string[] places = { "Johannesburg", "Yachats", "Seattle" };

var query3 = from place in terraPlaces
             where places.Contains(place.Name)
             orderby place.State
             select new { place.Name, place.State };
Here are the results:
{ Name = Johannesburg, State = California }
{ Name = Johannesburg, State = Michigan }
{ Name = Yachats, State = Oregon }
{ Name = Seattle, State = Washington }
{ Name = Johannesburg, State = Wisconsin }

Genevieve Orchard was the primary creator of these samples. She works on the docs team, and also wrote a wonderful walk through that can help you understand this sample.

Genevieve's walk through: http://msdn2.microsoft.com/en-us/library/bb546158.aspx

People interested in this topic might also want to read Matt Warren's in depth study of LINQ providers. Some of the code that Matt shows in his series of posts was included in Genevieve's sample.


kick it on DotNetKicks.com