26

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?

Andreas
  • 681
  • 1
  • 8
  • 16

2 Answers2

33

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 !

rama
  • 461
  • 4
  • 3
  • Does someone knows how to do this in Linq C# client?? – Andreas Furster Jan 12 '15 at 12:09
  • @AndreasFurster Did you find out? – LueTm Feb 05 '15 at 12:58
  • 2
    @LueTm Yes. If I remember it right the `.Expand(string entity)` method just adds `$expand=` to the url query. So you could do: `.Expand("Families($expand=Models)")`. Yeah it's a 'little' bit ugly... But i though it will work. – Andreas Furster Feb 05 '15 at 14:05
  • 1
    the URL convention still uses slashes in odata v4: http://docs.oasis-open.org/odata/odata/v4.0/errata02/os/complete/part2-url-conventions/odata-v4.0-errata02-os-part2-url-conventions-complete.html#_Toc406398162 – Jerther Feb 13 '15 at 13:58
  • For anyone looking for this in pre v4 of oData you can use it like this instead: ?$expand=Families/Models as mentioned in the specs https://www.odata.org/documentation/odata-version-2-0/uri-conventions/ – Axelle Dec 02 '21 at 13:10
32

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.

Gene C
  • 2,010
  • 26
  • 34
  • darn, I've read that ...but dismissed it rather quickly cause I had no idea what they meant with 'graph of entities' ... #$%$ Thank you very much for pointing me, Gene – Andreas Aug 16 '11 at 02:45
  • how do you make the select? – Leandro Bardelli Jun 04 '14 at 13:34
  • This syntax also works with the `QueryableExpandAttribute` https://gist.github.com/dbeattie71/7820af9f4ae4f68069207b49fc446c94 `[QueryableExpand("GrainBins,GrainBins/GrainBinLevelHistories")]` – Derek Beattie Jun 18 '16 at 16:58