Search code examples
c#.netwcf-data-servicesodatapage-size

Get the Next Page of a Sub Collection in OData (WCF Data Services)


I have an WCF Data Service (OData V3) that has a page limit of 100 items (for all entities).

Normally, if I need to move beyond that limit, there is no problem as I can just use the built in paging to get the next page of results.

However, I can't see any paging built into "sub results". And I need that badly!

Let me illustrate with an example data structure that is a list of Orders:

List<Order> Orders
    |
  Order #1:---
    |        |
    |        |-- Customer
    |        |-- List<Item> ItemsOrdered
    |                | -- Wigdet Type 1
    |                | -- Wigdet Type 2
    |                | -- Wigdet Type 3
    |                | -- Wigdet Type 4
    |                | -- Wigdet Type 5
    |                | -- Wigdet Type 6
    |                | -- Wigdet Type 7
    | 
  Order #2:-
             |
             |-- Customer
             |-- List<Item> ItemsOrdered
                     | -- Wigdet Type 8
                     | -- Wigdet Type 4
                     | -- Wigdet Type 2

If my page limit is set to 5, then I can see no way to query the rest of the ItemsOrdered List.

Is there a way to do continuations on "sub lists" in OData?


Solution

  • On the protocol level the inner feeds (or collections) will contain the next link, just like the top-level one. So all you need to do is issue a GET to that next link (it will continue enumerating the entities for that expanded entity, the right stuff is encoded in the next link).

    If you're using the WCF DS Client library you need to make sure that the type of the expanded property is DataServiceCollection. So in your case the client side type for the ItemsOrdered property should be DataServiceCollection (note that this is only required on the client, has nothing to do with how the data is modeled on the server).

    Once you have that the DataServiceCollection has a Continuation property which is the representation of the next link. To issue a request to load more, the most convenient way is to call context.LoadProperty(orderInstance, "ItemsOrdered", orderInstance.ItemsOrdered.Continuation);