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

3

15

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?

Dumfries answered 24/9, 2012 at 19:32 Comment(1)
@VladL above... (sorry, I don't have the rep points to comment), I don't think that works either. I just tried it and added Quantity to the select statement for the Order_Details: services.odata.org/V4/Northwind/Northwind.svc/… It does not appear that the Order_Details are being sorted by Quantity.Respite
R
16

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)

Rager answered 27/5, 2015 at 9:9 Comment(11)
the syntax is correct, but the nested orderby doesn't seem to change anything in the result.Benz
@Benz I've spent a lot of time with sample OData services from odata.org and noticed that you get different results at different times, if the service is not down at all ;) Try it with TripPin odata.org/odata-servicesRager
If the syntax is correct, it's weird that it doesn't work then :sHelminth
As far I understand, it would not be possible with the version 2??Maybe
@Maybe not even in V3Rager
But with the v2 is impossible?Maybe
@Maybe v1, v2 and v3 - impossibleRager
This syntax is correct but the problem is it doesn't sort the Orders it only sorts the order_details within each of the orders.Destrier
@Destrier sure, because it is applied to order_details :)Rager
@Rager In my $expand=az_Job($select=az_JobId,az_Code,az_Name;$orderby=az_Name desc), I have specify desc in $orderby, unfortunately it didn't work. What did I miss?Gorrian
Can I ask you where did you find that syntax rule? I mean the comma to separate more statement inside an $expand. Thank you.Abbie
B
14

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

Blinker answered 24/9, 2012 at 20:53 Comment(0)
S
2

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

Stridor answered 18/10, 2020 at 20:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.