Search code examples
wcf-data-servicesodata

OData - Limit the number of related entities


Is it possible to make this kind of request with OData ?

"Get All Customers with theirs last 5 Orders"

Thanks for your help!


Solution

  • Not with OData v3. OData v4 has support for this. The syntax would look like,

    ~Customers?$expand=Orders($orderby=OrderID&$$top=5)
    

    Refer to the V4 draft here.

    You could always do it with multiple requests though with OData V3. Using the $batch feature would let you do it in 2 requests, get all customers first and then build a $batch request with GET ~/Customers(id)/Orders?$orderby=CustomerID&$top=5 for each customer in the first request.