user466512
user466512

Reputation: 147

OData $orderby clause on collection property

I have the following classes :

 public class Parent
 {
     public string ParentProp { get; set; }
     public IEnumerable<Child> ManyChildren { get; set; }
 }
 
 public class Child
 {
     public string ChildName { get; set; }
     public int Value { get; set; }
 }

Say I have an OData operation defined which returns IEnumerable<Parent>. Can I write an $orderby clause which performs the following operation ('parents' is an IEnumerable<Parent>) :

parents.OrderBy(x => x.ManyChildren.Single(y => y.ChildName == "Child1").Value);

I know I can write custom actions (http://msdn.microsoft.com/en-us/library/hh859851(v=vs.103).aspx) to do this ordering for me, but I'd rather use an $orderby clause.

(The only SO question which asked something similar is a little dated - How can I order objects according to some attribute of the child in OData?)

Upvotes: 1

Views: 5580

Answers (2)

Marko Rakić
Marko Rakić

Reputation: 116

As I tried is possible with nesting $orderby in $expand so will be:

odata/User?&$select=Active,Description,Name,UserId&$expand=Company($select=Active,Name,CreatedBy,CompanyId;$orderby=Active asc)

And what you get is somthing like:

ORDER BY [Project2].[UserId] ASC, [Project2].[C19] ASC

will order a company collection for each user separately.

I think in version OData Client for .NET 6.7.0 is supported, in release notes is writhing:

In query options $id, $select, $expand(including nested query options)....

I see in version 6.1 that values for nested options exist and is in:

DataQueryOptions->SelectExpand->SelectExpandClasue->SelectedItems->ExpandNavigationItem->OrderByOption

but is not working.

I tried and with System.Web.OData 5.6 and all releated dependencies but seams is not working.

My conclusion:

Seams that is everiting prepared like DataQueryOptions exist nested orderby but is not working.

Like I find out standard seams is going in that direction. https://issues.oasis-open.org/browse/ODATA-32

Upvotes: 1

Maya
Maya

Reputation: 836

It depends on your OData service implementation. Which kind of service are you using? WCFDS, WebAPI, or the service you implement yourself?

Url parser do can parse the URL such as root/People?$orderby=Company/Name. The translator is implemented by service.

And I agree with the answer in related question: "it's not possible to do this with a navigation property that has a cardinality of many". Since it's has a cardinality of many, service cannot know which one should be used to sorting.

Upvotes: 0

Related Questions