Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to expand multiple properties on OData

Tags:

odata

Consider I have this OData expression:

http://services.odata.org/northwind/northwind.svc/Categories?
    $expand=Products/Category

It will correctly expand the Products.Category.

Now I want to expand another property too. For example 'Products.Supplier`.

I've tried duplicating the $expand usage:

http://services.odata.org/northwind/northwind.svc/Categories?
    $expand=Products/Category
    &$expand=Products/Supplier

but it failed returning this error:

Query parameter '$expand' is specified, but it should be specified exactly once.
like image 397
mehrandvd Avatar asked Jun 01 '14 06:06

mehrandvd


People also ask

What is $value in OData?

The $value option is used to get individual properties of an Entity. There are two ways to get individual properties from an entity. We can get the response in either OData format or get the raw value of the property. We need to add method to the controller named GetProperty here property is a name of the property.

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 OData filter?

The $filter system query option allows clients to filter the set of resources that are addressed by a request URL. $filter specifies conditions that MUST be met by a resource for it to be returned in the set of matching resources. The semantics of $filter are covered in the OData:Core document.


2 Answers

According to OData ABNF, expand syntax should be:

expand = '$expand' EQ expandItem *( COMMA expandItem )

Which amounts to:

$expand=expandItem1,expandItem2,expandItem3,...

So please try:

http://services.odata.org/northwind/northwind.svc/Categories?$expand=Products/Category,Products/Supplier


For more information, see:

http://www.odata.org/documentation/odata-version-2-0/uri-conventions/#ExpandSystemQueryOption

like image 116
zoe Avatar answered Sep 28 '22 12:09

zoe


You can also try this syntax for expanding multiple levels:

$expand=Products($expand=Category),...

This works well with MS OData implementation in WebAPI.

like image 38
Marek Avatar answered Sep 28 '22 13:09

Marek