Starting from today I will try to review some of the tools that work well with the Open Data Protocol. Idea is to bring out the goodness these tools provide to work with OData or Open Data Protocol. So if you are also interested in knowing the tools sit back and read on :).
Today lets take a closer look at a tool called as "LINQPad":
LINQ Pad is a tool which allows you to write .NET LINQ queries against data sources like Database, Objects, XML etc. Here is the official word from the developers of LINQPad:
LINQPad lets you interactively query databases in a modern query language: LINQ. Kiss goodbye to SQL Management Studio! LINQPad supports everything in C# 4.0 and Framework 4.0:
- LINQ to Objects
- LINQ to SQL and Entity Framework
- LINQ to XML
- Parallel LINQ
LINQPad is not limited to just the above said technologies – it supports OData/WCF Data Services, Windows DataMarket, SQL Azure etc.
Getting the tool:
Official site for LINQPad is www.linqpad.net. LINQPad Standard Edition is free to download and it is an single executable of 3MB footprint. LINQPad is not an open source product and that means, the source code is protected by standard copyright laws. But as the developers have put it "you are free to disassemble the executable to satisfy your curiosity" :). Download the tool and extract it to a folder. You will see an EXE and a CONFIG file. And that’s all there is to the download. Lets see how to get started with the tool.
Double click the LINQPad.exe from the folder where you extracted it. This will launch the tool. If this is the first time you are using the tool, it will look like below:
Left hand top corner is known as Connection Workspace. On the right hand side of the interface is the Query Workspace. Here is where you will write the LINQ queries.
Connecting to OData Service:
LINQPad works with the concept of Connections. To work with a particular data source you would need to first create a connection to that data source. In this case we intend to query a OData/WCF Data Service. So lets add a connection to one such service. We will be using one of the live services available at OData.org which is for classic Northwind database. Here is the live service URL: http://services.odata.org/Northwind/Northwind.svc/. You can connect to any OData from LINQPad – Northwind is just one service I am using as an example. You can create multiple connections in LINQPad. Lets see the steps to create a connection:
Step 1: In Connection workspace, click on "Add Connection". We will see "Choose Data Context" dialog as below:
Step 2: Select "Build Data Context Automatically" Radio button. Then select "WCF Data Services (OData)" LINQPad driver.
Step 3: We will be presented with "WCF Data Services (OData) Connection" dialog. Provide the URI of the OData Service in the URI section. Since this sample service does not contain any authentication mechanism we don’t have to provide any Username and Password. So leave them blank. You can select Remember this connection so that it gets cached by LINQPad.
After finishing Step 3, here is how the Connection workspace looks like:
Executing OData Operations:
As you can see from step 3 – once you connect to a OData Service, LINQPad will retrieve all the entity data sets and show it in a tree structure. If you right click on any entity data set you will be able to get the following context menu:
Here I have selected Categories entity set and on right click of that node we get the following options:
Clicking this option will get a LINQ query in the Query workspace and automatically executed.
Similar to first option except that this will wait for your input for the Take function. We can provide a value to take and then click on execute by pressing F5.
This option will write a LINQ query to get the count of Categories.
from c in Categories where .,. select c
This option will output a LINQ query where you will need to provide a where clause. Check the screen shot below:
That’s all its there to execute a OData service operations using LINQ Pad.
Executing Custom Query:
What we saw in the previous section was the application provided LINQ query to speed up things. You can modify those queries if you want to or write your own custom query. To execute custom query you can do following:
– Press Ctrl + N from the keyboard
– Right click on the OData Service Root Node in the Connection Workspace and select New Query.
This will create a new Query Workspace and you can write your Custom LINQ Query, execute and get the results.
One thing that’s so special about LINQ Pad is it lets you write LINQ Queries but under the hood the LINQ query is converted to ODATA URI and then executed. To see this all you need to do is after executing the results click the SQL toggle button just above the Results workspace. Here is the screenshot:
As you can see what we wrote as a LINQ query has been converted into ODATA Service URI and then executed, This is really cool feature of LINQPad that I am fan of. If you feel like writing the OData URL for a complex data retrieval then you quickly fire up the LINQPad write a LINQ query and grab the URI and use it. Well that’s what I do sometimes
In my endeavor to learn OData I have been trying to see what tools are currently available. LINQPad is one awesome tool to learn LINQ and also use a scratch pad to execute OData operations and quickly see the data set and its structure. I highly recommend you to make this as a scratch pad in your day to day activities.
What I have covered today in this article is focused towards LINQPad’s ability to support OData out of the box. There are many more features of LINQPad that will blow your mind off. Hope this article gave you some idea on LINQPad and its usages. Give it a try!
Till next time, as usual – Code with passion, Decode with patience.