OData Query syntax

If you have setup WCF Data Service, you can now query your service with the OData syntax. If you are using Entity Framework, you will see a lot of similarity between Entity Framework and this string syntax. OData works with http request which is based on URI. Every queries statements need to be passed by this url. Here is a list of all possibles statement that can be used together as Entity Framework or a SQL statement.

OData ordering keyword : orderby

If you request one of your entity set, you may want to order the result. To be able to order, you will have to user orderby with = and the property of the entity. If you need multiply properties in your ordering, simply seperate them with a comma (,). Here is two examples.



OData getting a limited amount of data keyword : top

This is the same keyword as Sql Server or Entity Framework. This let the consumer to ask to the WCF Data Service how many result maximum the consumer receive. It can be the specific number or less, or none. Here is an example.


OData paging result keyword : skip

You can do paging with OData. For example, if you want to have a list that display 10 elements per page, you could request the WCF Data Service to get the first 10 entities and then ask the next 10 entities when the user request the next page. This can be done with the previous keyword top to get the 10 entities but to get which group of 10, we need to new keyword skip. If you want to have the third page of 10 elements, we will need to use skip with the value 20 and the top at 10. This can be translated by : “I want the third page of 10 elements”. The value of skip is 20 because we have 10 elements by page. One page mean we display 10, two pages mean we display 20. The third page requires to skip those 20 pages and to display the next 10. This is specify with the top keyword.


Condition with OData by the filter keyword

In SQL, we would use the keyword where and the keyword and if we have multiple conditions. It’s the samething with Entity Framework. But, with OData, the keyword is not where. The keyword is filter.

http://yourwebsite/YourService.svc/YourEntitiesSet?$filter=YourProperty equal('Test')

OData contains several keyword to compare : http://go.microsoft.com/fwlink/?LinkId=186972

Joining entity with the keyword expand

Entity Framework gives the keyword include which add a reference to an other entity or a list of entities. You need to specify the name of the property and this one will be loaded in the foreign key that define the relationship in the database. With OData, the keyword is expand.


Selecting specify properties of an entity with the OData keyword select

This is exactly the same as SQL and Entity. As you can see since the beginning of this article, a lot of keyword in OData are the same with Entity and SQL. This is a strong positive point that give a low learning curl. The select keyword give you the posibility to not returning the whole entity information but only specific property of this one.


WCF Data Services is the implementation of OData by Microsoft

Several time a week I hear confusion concerning WCF services, WCF Data Services, ADO Data Service and OData. First of all, ADO Data Service is out. In fact, it’s the old name for WCF Data Service which is the good sentence for OData by Microsoft. WCF service focus on contract when Data Service focus more on the data. OData is been invented by Microsoft but is not the implementation in .Net. It’s the protocol which is open.

WCF Data Service is a type of service that provide information through url. This is easily consumable because it requires only a web browser to execute a call which can have filter, join between entities and conditions. WCF Data Service uses the entity-relationship conventions of the Entity Data Model to expose resources as sets of entities that are related by associations. Not only can you get information the way you want but you can also update entity and insert.

WCF Data Service lets you use two well know structured semantic which is Atom (XML) and JSON. Both of these protocole are RestFul. That mean that when a call is executed, the life cycle end once the data is sent back to the consumer.

To create a WCF Data Service, simply create a class that inherit from DataService. It’s a generic class so you will have to specify a DataContext type. To use this class, you need to reference the dll : System.Data.Services.

public partial class MyService: DataService<MyDbContext>

You will have to also specify which entities is allowed to be accessed in InitializeService.

public static void InitializeService(DataServiceConfiguration config)

The simplicity lets you add this service in a later stage of your project. It’s even more true if your project is already using Entity Framework because OData is tightly coupled Entity Framework way to handle entity. This is why it’s very easy to create a WCF Data Service with Entity Framework because of the Data Model that both share.

OData is between the consumer and the source, the consumer could be a simple web browser and the source a .Net application or SQL Azure server. This is very powerful because you can make a mobile application that use the same service as a web site or a winform. Even more, you can create on different web browser (Internet Explorer, FireFox, Chrome) or different mobile platform (Android, IPhone, Windows Phone) with the same WCF Data Service.

The OData protocol lets you send more than only GET request. GET http request will only give you the possibility to receive data. In the case you want to delete data, a DELETE http request would be required. POST is used to insert, PUT to update. The protocol uses string and not a reference, this is why XML and JSON is used. It also respect the concept of not being tight to the server by being RestFul. So in short, a SQL table (rows) is converted into Entity Set (entity) which is converted to a collection of entry (entry).

WCF Data Services also supports data models that are based on common language runtime (CLR) objects that return an instance of the IQueryable interface.