Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to Query an Odata service and expand Child of Child entities?

Tags:

This sounds rather simple (and maybe I'm missing the obvious here) but I can't find a solution. I know I can query an entity and return one, or many direct child entities doing this:

var query = from c in Service.Clients.Expand("Addresses,Comments,PhoneNumbers")..

What I would like to be able to do is do the same with 3 levels (Children of child), lets say "Country->Province->City" or "Brand->Family->Model"

I tried to expand all entities, but it fails

var query = from c in Service.Brands.Expand("Families,Models").. //fails,
//which even makes some sense, since Models is a Child of Family, not Brand
var query = from c in Service.Brands.Expand("Families").. //this works, 
//but Family.Models is empty

Is there a way to do this in one query, or do I have to split this in two separate queries?

like image 557
Andreas Avatar asked Aug 15 '11 19:08

Andreas


People also ask

How do you use $expand in OData?

OData query option $expand is used to read multiple entities or entity sets in a single service call instead of two different calls. Prerequisite, entity sets which are used should be associated. To know about Association in OData service click here.

What is $select in OData?

The $select option specifies a subset of properties to include in the response body. For example, to get only the name and price of each product, use the following query: Console Copy. GET http://localhost/odata/Products?$select=Price,Name.

What is the OData protocol?

The OData Protocol is an application-level protocol for interacting with data via RESTful interfaces. It supports the description of data models, editing and querying of data according to those models.


Video Answer


2 Answers

In ASP.NET OData v4, the URL convention seems to have changed. It is now:

~/Brands?$expand=Families($expand=Models)

You can also select the stuff you want in sub-entities.
For example if you want just the brand family identifiers:

~/Brands?$expand=Families($select=Id)

Furthermore, if you want only the identifiers of the brand family models, you would do that:

~/Brands?$expand=Families($expand=Models($select=Id))

...and so on. Hope this helps !

like image 60
rama Avatar answered Oct 14 '22 15:10

rama


The following statement should return what you are looking for:

var query = from c in Service.Brands.Expand("Families/Models")

This will run the following odata query:

.../OData.svc/Brands?$expand=Families/Models

Here is a link to some further odata documentation:

The syntax of a $expand query option is a comma-separated list of Navigation Properties. Additionally each Navigation Property can be followed by a forward slash and another Navigation Property to enable identifying a multi-level relationship.

like image 35
Gene C Avatar answered Oct 14 '22 16:10

Gene C