If I use my entity model in a web project I can navigate to the 1-* 1-0.1 nav properties fine…but when I load that exact same object via LinqPad at my oData Service the nav property is always null
…am I doing something wrong?…should it be enabled somehow?
If I load up fiddler and run the query http://odata.site.com/Service1.svc/usda_FOOD_DES(1001)/usda_ABBREV
…it returns the correct result
Thanks,
Steve
I asked myself the same question. I was wondering why the following returned an empty collection:
The reason is that unlike with Entity Framework’s navigation properties, when you access a property of an OData entity it does not automatically go back to the data source and retrieve any data that is not already present. So when you retrieve an entity from an OData feed, by default it does not include the linked entities, so you get an empty
IEnumerable<T>for one to many or many to many navigation properties or null for single entity navigation properties.You would expected the above query to translate to:
Which would return all orders, but instead it translates to this (to see the query in LINQPad, click the SQL button above the results pane):
This is because the
.Ordersat the end is not within any extension method, and so does not participate in the construction of the IQueryable. Therefore it is not reflected in the URL that is constructed from the query and is not included in the result set.So how would you get just the orders of customer 1? You could try and work around the issue by using the following query:
This should cause the
.Ordersproperty to be included in the query. Unfortunately, a.Select(...)without projecting the result into an anonymous method (i.e.new { ... }) is not allowed and throws aNotSupportedException: The method 'Select' is not supported.What a shame. So what about:That doesn’t produce the expected result and instead performs the following query:
I don’t understand why this is simply not translated to /Customers(1)/Orders. In any case, it puts the list of orders inside an unneeded wrapper class which can be quite annoying, but it works, and it was the closest I could get to retrieving just the content of a navigation property.
The method I prefer is to include all the orders with each customer, as so:
This produces the following query:
This works, but has the disadvantage of including all the customer details, which we may not need. Also, the string-yness of
.Expand(...)is problematic – it is has weak typing so it won’t be verified by the compiler (beware of spelling mistakes), not all refactoring tools will refactor it too, finding all usages of that property will not include that string in the search result, etc.