Search code examples
c#vb.netodataodata-v4

OData client v3 to v4 update approach for Expand object of collection


From some thirdparty we use the backend which been updated from OData v3 to v4. That means we have to update our software as well.

Now the problem i'm facing is that the way collections work are different in v4 than they where in v3. For example, the code i used to GET ~/OData/Orders and also the OrderItems and in the OrderItems collection expand the object Product was:

SomeDataServiceContext.Orders.Expand("OrderItems").Expand("OrderItems/Product").Take(10)

Now with OData v4 i can expand the OrderItems, but the expand of the Product object within the OrderItems collection don't work anymore. This is the code i now use:

SomeDataServiceContext.Orders.Expand(Function(x) x.OrderItems).Take(10)

But as you can see this does retrieve the collection of OrderItems but don't expand the Product object within the OrderItems collection. The OrderItems Collection however does contain the property ProductId

So in this case what would be the right approach:

  1. Find if there is a new way in v4 to get nested Expands
  2. Query Endpoint SomeDataServiceContext.Products to get the corresponding product
  3. Suggestions?

My knowledge about OData is beginner to medium level, so be gentle ;-)


Solution

  • From the client there is lambda syntax support for nested expansions only if the link is singluar, as Order.OrderItems is a collection you can use the string variant of the expand method:

    SomeDataServiceContext.Orders.Expand("OrderItems($expand=Product)").Take(10)
    

    You will notice here, expansion syntax is one of the breaking changes in URL parsing between OData v3 and v4.