Geeks With Blogs

News Welcome to my programming blog. I am a software developer living in Hertfordshire, United Kingdom.

I've been a keen developer for over a decade, most of it spent on C#/.Net development with ever increasing amounts of Javascript and I am an avid biker as well.

Please feel free to leave a comment if you found a post useful :-).

My twitter handle is BikerDev.

My LinkedIn profile.
Ritesh Singh's Programming Blog .Net and more

If you've used ASP.Net MVC then you may be aware of Web API which is Microsoft's preferred framework for developing restful web services. It is far superior to something half baked like the WCF REST Starter Kit. WebAPI even has an HttpClient class with a 'self host' to run integration test scenarios.

For instance if you're writing a service to get a list of products it might look something like this... mysite/myservice/products

Now what if you wanted to filter the results to all products whose id is less than 10 and order that by units in stock. Normally this would require filtering the result set but by using OData this can be done either at the service level in memory or database level if you're using Entity Framework. With OData additional querying and filtering is now possible.

In order to get OData support in your Web API service you'll need to add the nuget reference available here

Once you add the reference you'll need to add the Queryable attribute to your web api method and ensure that the return type is IQueryable.



The examples included below are of a web api service using entity framework 5 to get Product data from Northwind.

Some of the expressions available to query and refine the result set are as follows...

$filter operator to further filter the result set and is equivalent to a Where clause.

In order to get all products whose Id is less than 10 the url would be as follows...

localhost:33326/api/Product?$filter=ProductID lt 10


$orderby is used to order the result set. URL to get products with ID less than 10 and then order by UnitsInStock is as follows…

localhost:33326/api/Product?$filter=ProductID lt 10&$OrderBy=UnitsInStock




As an aside I highly recommend using jsonlint to format json data.




You can use the $top and $skip operators for paging


Note - Be aware that spaces in between operators messes with the result. Please ensure that there are no spaces after the '&' separator used between filter operators


If you're using Linq to Entities in Entity Framework you get the added benefit of deferred execution which means that the entire query is run at the database level and only the filtered result set is returned which is more efficient in terms of volume of data.

The screenshot below is of the first filter query when run by sql server.



$select can be used to get related entities and $expand to include a subset of the result. This was introduced with .Net 4.5.1


OData can also be used to update data and there are of course security considerations when one can query via a URL but these are topics for another blog post.


As you can see OData can add a lot of flexibility to your web api calls and result in improved performance as well.

Posted on Tuesday, September 17, 2013 5:21 PM C# , ASP.Net | Back to top

Comments on this post: OData in WebAPI

# re: OData in WebAPI
Requesting Gravatar...
Kaatilaana concepts
Left by Jawahar Kumar on Sep 18, 2013 5:00 AM

Your comment:
 (will show your gravatar)

Copyright © BikerDev | Powered by: