Search code examples
asp.net-web-api2odataodata-v4

OData V4 $expand nested $filter and $orderby -- combine $filter and $orderby inside expand


I currently have an OData V4 service that has the following model.
"Category" -- "Codes"
For each category there can be many codes.

I need to $expand the Codes, $filter where Active = true and then $orderby Codes.Description.

Currently the following works fine without ordering.

odata/Categories?$expand=Codes($filter=Active eq true)

This does not work.

odata/Categories?$expand=Codes($filter=Active eq true&$orderby=Description)

I receive "The query specified in the URI is not valid. Syntax error at position 12." & "Syntax error at position 12 in 'Description)'."

Basically it is reading the last ")" after "Description" as an error.


Solution

  • I was not able to find this in the OData V4 documents but I did find a similar example here "How can I order objects according to some attribute of the child in OData?"

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

    The key I was missing is

    ;

    This goes between the filter and orderby.
    Here is the working url

    odata/Categories?$expand=Codes($filter=Active eq true;$orderby=Description)