Part 06 – OData v4 in ASP.NET WebApi – Querying 2

This is a series of blogs detailing my introduction to using OData v4 with C# and ASP.NET WebApi.

Query Entity By Key

The previous blog showed how we could list the Airlines entity set. The AirLineCode is a key and therefore the value is unique. We can list a single entity by referencing its key.

From Postman let’s look at the Airline ‘AA’. Issue a GET request to http://services.odata.org/TripPinRESTierService/Airlines(‘AA’)

A single entity is returned along with all the properties defined in the model. We cannot filter on ‘American Airlines’ because Name is not a key.

Fetch a Single Property

From Postman let’s fetch just the Name of the Airline. Issue a GET request to http://services.odata.org/TripPinRESTierService/Airlines(‘AA’)/Name

The Name property is returned only however; the JSON has given the field the name value. You can only return a single property using this resource path.

Fetch Entities using a Navigation Path

The Person entity has three Navigation Properties. These are relationships from one entity to another.

From Postman let’s fetch a single Person entity. Issue a GET request to http://services.odata.org/TripPinRESTierService/People(‘russellwhyte’)

All the properties are return however; there is no reference to any navigation property. Lets see what trips this person has been on.

From Postman let’s fetch the trips for Russel White. Issue a GET request to http://services.odata.org/TripPinRESTierService/People(‘russellwhyte’)/Trips

Here three trips are returned. Since TripId is the key defined on the Trip entity, we can filter out a single trip.

From Postman let’s fetch the trips for Russel White. Issue a GET request to http://services.odata.org/TripPinRESTierService/People(‘russellwhyte’)/Trips(0)

Query Entity By Composite Key

Although the Trip Pin service does not define a composite key, you can define a model to have one. For example; imagine a Person entity that has two keys, FirstName and LastName. They are only unique when combined together and not unique individually.

The URL would look like http:////Person(FirstName = ‘Joe’, LastName = ‘Bloggs’)

We need to specify all the property names when two or more keys are used.

 

In the next blog we will actually do some coding and develop a simple service to query using single and composite keys.