Work With Odata in Web API : Querying Odata Service

In this article we will discuss a very important topic of Odata; how to make a query to an Odata service. I strongly believe that if you understand the concept of queerying an Odata service then only you will realize the real power of Odata services.

Welcome to the “Work with Odata in Web API” article series. In our previous two articles we discussed the basic idea of Odata services and the implementation of Odata in the Web API environment. If you skipped them then please do visit them in the following links.

  1. Work With Odata in Web API : Introduction of ASP.NET Odata service
  2. Work With Odata in Web API : Create your First Odata Service

In this article we will discuss a very important topic of Odata;  how to make a query to an Odata service. I strongly believe that if you understand the concept of queerying an Odata service then only you will realize the real power of Odata services.

In our previous discussion we said that Odata provides the ability to query data from the client side. So the advantage is that a client program can make a query of their own, without depending on the server results.

And this is the philosophy behind the Odata implementation. Fine, we have explained the concept. Now we will learn a few basic queries to get data from the Odata service.

Oh, the good news is that we need not implement the Odata service on our own. To understand the concept of various queries, in this article we will use one public Odata service. Here is the URL:

services odata Products

A point to make is that the search query or the filter we need to pass through the URL in the case of the Odata service. So the URL may not be a RESTFul URL like MVC and to consume data from this service we will use a browser, if needed you are free to use Fiddler.

Fetch all data

At first we will learn to fetch all data using a query. Surprisingly there is no need to pass a query string to fetch all the data. Just call the URL and Cheers!

services odata Products

Here is sample output. Oh, really the service has thrown a bulk of data and to make the article lightweight (haha..:) I have not taken the entire screen of output.



Get data in specific format

We can request a data type to the Odata service using a “$format” filter. The URL is something like the following.

services.odata products json

And we will get data like the following screen. Anyway I have not taken a screen shot of the entire output screen. We are seeing that the filter parameter is taking “json” as a value, instead of json you may provide “xml”. And please keep in mind that the service should be capable of returning various data formats.

Otherwise it will throw a media type format exception. In the output we are seeing JSON data as output.



Get specific data

If you are not really serious still then I request you to be serious at least in this point. Here we will learn to get a specific product and I think this bit of knowledge will help you to understand the real power of Odata services.

To get a specific product information from a service we need to pass the item number within brackets. In this example we are fetching the first product and for that we have passed “1” as the parameter.
 


Specific product in specific format

Oh, you got it. Yes from our previous knowledge we will combine a query string to get a specific product in a specific format.

In an example we will fetch the first product in JSON format. Here is the formatted URL.

services odata json
 


So we are getting the first product in JSON format. Nice and cool.

Get part of sub category within the category

If needed we can fetch some part of the sub category under another category. Have a look at the following example.

services.odata.Products

The URL is saying that we are interested in getting the first category from the Product main category.  Here is output in XML format.
 


Get column using projection

This is another important query to consume data from an Odata service. We know it is a select query with column name in SQL. In this example we will do a very similar operation.

services odata Name

We are interested in fetching the first product from the service and from that we are filtering only the ID and Name columns.
 


So, the advantage is that, we can fetch data depending on our needs. We will not depend on the service's data.

Fetch top n result using query

In our previous example we saw how to get a specific product, in this example we will learn to retrieve the top n results. Here is the query to get the top 2 products.

services odata top=2

Yes, if needed we can get data in JOSN format. Here is a sample query.

services odata json top 2
 

 
Relational operation

We can use a relational operation like greater then, less than and many more as in the following query. Here the product whose ID is less than 4 will be fetched.

services odata ID 4
 
 

In the same way we can use “ge” for greater then “ne” for not equal.

Conclusion

I am very confident that you now have the idea behind Odata services and you now realize the real power of Odata services.