smooth_smoothie
smooth_smoothie

Reputation: 1343

How can I order objects according to some attribute of the child in OData?

I'm trying to do some custom sorting in OData using this URL

localhost:82/odata.svc/ComponentPresentations?$filter=TemplateId eq 2894 and publicationId eq 10&$expand=Component/Keywords?$orderby=Title desc

Where Componentis a property of ComponentPresentation and Keywords is property of Component, and I want to sort the ComponentPresentation according to the keyword's Title attribute. But keywords nor title is a property of Component Presentation

Is there a way to sort the results according to the attribute of Keword's title? Which is a Child of Component, which is a child of ComponentPresentation?

Upvotes: 16

Views: 15795

Answers (3)

Soheil Bijavar
Soheil Bijavar

Reputation: 83

append order by with expand object/field on the end of the query

localhost:82/odata.svc/ComponentPresentations?$filter=TemplateId eq 2894 and publicationId eq 10&$expand=Component/Keywords?$orderby=Component/Title desc

Upvotes: 2

VladL
VladL

Reputation: 13043

Just want to mention that it is possible since OData V4. You can nest as many expands/selects/orderby/filters as you wish. Now it is as simple as

http://services.odata.org/V4/Northwind/Northwind.svc/Orders?$select=OrderID&$expand=Order_Details($select=UnitPrice;$orderby=Quantity)

Upvotes: 16

Mark Stafford - MSFT
Mark Stafford - MSFT

Reputation: 4336

It is possible to sort the results by a nested single-cardinality property, for example: http://services.odata.org/V3/Northwind/Northwind.svc/Orders?$top=50&$expand=Customer&$orderby=Customer/City

AFAIK, it's not possible to do this with a navigation property that has a cardinality of many (though it would be useful in expand scenarios to order what comes back in the expanded feed).

Upvotes: 14

Related Questions