Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 340
smooth_smoothie Avatar asked Sep 24 '12 19:09

smooth_smoothie


People also ask

What is filter in OData?

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.

How do I query OData service?

You can find details on filter specification in the OData spec filter options section. Examples: All products with a Name equal to 'Milk': http://host/service/Products?$filter=Name eq 'Milk' All products with a Name not equal to 'Milk' : http://host/service/Products?$filter=Name ne 'Milk'

How does OData query work?

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.

What is OData URI?

Introduction. The Open Data Protocol (OData) enables the creation of REST-based data services, which allow resources, identified using Uniform Resource Identifiers (URIs) and defined in a data model, to be published and edited by Web clients using simple HTTP messages.


2 Answers

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)

like image 120
VladL Avatar answered Oct 21 '22 12:10

VladL


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).

like image 32
Mark Stafford - MSFT Avatar answered Oct 21 '22 14:10

Mark Stafford - MSFT