Monday, March 18, 2013

Use LINQPad to Query an OData Service

This is going to be a brief post just to get you going with using the free LINQPad tool to query an OData Service. 

The 1st step is to download LINQPad if you have not already done this from the following link: LINQPad Free Download

You will also need WCF Data Service 5.0 installed if you are planning on actually creating your own WCF Data Services to be built in VS.NET and then running this tool against. You can find that download here: WCF Data Services 5.0 for OData V3

Once LINQPad is installed go ahead and open it up and press the 'Add connection' link button in the top left-hand corner.


Select the 'WCF Data Services' LINQPad Driver and select 'Next':



The easist OData service to connect to is the Netflix data service which is exposed at the following URI: http://odata.netflix.com/Catalog/ Enter this value in the URI and leave all of the other fields as their defaults indicate. There is no authentication to access the public catalog of Netflix. Press 'OK'.



Take a look at the tree structure built displaying the catalogs available from Netflix. This is how we know the connection was successful.



Now we want to create a simple C# LINQ query and see the multiple pieces of functionality we can extract. Right-click on the 'Titles' catalog and select the suggested 'Titles.Where(t => ...)' which will load this expression into the query window. Finish it off by searching for the movie Top Gun as follows:


Titles.Where (t => t.Name == "Top Gun")



Press the 'Execute' button and see the results:



Take a look at the different ways we can view the results. Press the 'Request Log' button to get the following URI with query string parameters which could be used directly in a browser:

http://odata.netflix.com/Catalog/Titles()?$filter=Name eq 'Top Gun'



Had we written a LINQ query instead of a lambda expression, we would be able to press the lambda button to then reverse engineer the lambda expression from the LINQ query we wrote in the window. There is other functionality like viewing the IL as well.

2 comments:

  1. Hi Allen,

    I need to join 2 service and pull data using LINQ, is there way using linqpad to generate query? or if tehre is any approach let me know.
    Thanks
    Lakshmi

    ReplyDelete
  2. LINQPAD will not let you connect to ODATA services in the same query window.
    You'll need to write a C# program.

    ReplyDelete