Using OData and LinqPad 4 to Create Your URIs for You

So last month I attended the Richmond .NET User Group meeting where the topic was OData, something that I have of course read about but had never utilized. Besides, attending this meeting would give me a reason to play around with it, hence this post :) .

Okay we all know that OData (Open Data Protocol) is a Web protocol that can be used to query and modify data. So now that that’s out of the way, this post will cover using OData in a Web application, using LINQ Pad 4 to create URIs for us, and displaying movie information retrieved from the Netflix OData feed.

Creating a WCF Data Service based off of an Entity Data Model
I started off by creating a new Web Application in Visual Studio 2010, adding an Entity Data Model based off of the Northwind database, and then adding a WCF Data Service named Northwind.

WCF Data Service Added To Project

By default the code-behind of a WCF Data Service contains a single InitializeService method. This is where you place the rules for accessing your entities:

Default WCF Data Service Code- Behind

The next step is to handle the TODOs, which means:

1. Removing the TODO from the implements statement and adding your data source class name

DataSource Class Name Added

2. Setting rules for your entities in the IntializeService method.

By default all entities are locked down in a WCF Data Service so users do not have access to them. So if I set the Northwind.svc file as my start up page and run the application all I’ll receive is default information via the default URI http://localhost:3574/Northwind.svc/:

Default  Data Service XML

You change this by adding access rules for individual or all entities. If the same rule applies to all entities in your model you can use an asterisk (*) to denote this i.e. if I wanted to allow someone read access to all of my entities I would add the following access rule:

Read Access For All Entities

Now if I run the application I receive a listing of all of the entities in my model:

Read All Access to All Entities

If I want to view Categories I could simply use the following URI: http://localhost:3574/Northwind.svc/Categories and receive the following results:

Category results

Let’s look at a particular entry:
Category Entry

Notice that the unique ID for an entry is a URL and column values are listed in the properties section.

We can control the type of access that users have to our entities using the EntitySetRights enumeration which has the following options:

EntitySetRights enumeration

URI’s and Querying
If I want to retrieve a particular category by ID I would use the following URI: http://localhost:3574/Northwind.svc/Categories(1) which yields the following:

Categories by ID

The following query retrieves products for Category 1 and orders the result set by ProductName and Unit price:

order by example

Results:
OrderBy results

Using filters ($filter)
Ex. Retrieve a list of product names and associated prices that have a unit price that is greater than $20:

Select and Filter URI

Results:
Select and Filter results

Creating URIs via LINQ queries using LINQPad 4
1. Add a new Connection
2. Select the WCF Data Services (OData) driver

LinqPad 4 Driver Option

3. Click Next
4. Enter the URI to your service. Here we will use the Netflix OData Catalog API (Preview)

LinqPad 4 Connection to Netflix OData Service

5. Click OK
6. You should now see the service reference listed as a connection

Netflix OData Service Entities

Now that you have your connection you can simply write LINQ expressions and select the SQL tab option to view the URI.

Example: Retrieve a list of movie titles released before 2000 LINQ query results:

Movies Released Before 2000 Linq results

LINQ Generated URI:
Movies Released Before 2000 - URI

Results when the above URI’s placed into the browser:
Movies Released Before 2000 - Browser Results

Example of a simple UI that displays Netflix OData feed results
In this example I’m simply calling a method that retrieves top 5 movies released after 2009 in alphabetical order. I am then displaying the cover, title, and synopsis of each movie returned.

Top 5 Movies Released after 2009 UI example

The Wrap Up
As you can see OData allows you to query data entities via URIs and you can utilize LINQPad 4 to generate URIs for you by simply writing LINQ queries.

avatar

About Tamika Harris-Smith

Tamika Harris-Smith is a Senior Project Lead (Consultant) for The Computer Solution Company (TCSC), and has been developing applications using the Microsoft .NET technology stack professionally since 2005.
This entry was posted in .Net, Linq to SQL, Visual Studio, WCF. Bookmark the permalink.

One Response to Using OData and LinqPad 4 to Create Your URIs for You

  1. I actually Think that blog post, “Using OData and LinqPad 4 to Create Your URIs for
    You | TCSC Blog” ended up being great! I actuallycannot agree along with you more!
    At last appears like I reallyuncovered a blog page truly worth
    browsing. I appreciate it, Davis

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>