Pergunta

I have an odata service that returns a parent with a child collection. Something like Order.OrderDetails.
On the client I have tried:

From x in context.Order.Expand("OrderDetails") Select x.OrderDetails[0]

"Error translating Linq expression to URI: The method 'Select' is not supported."

From x in context.Order.Expand("OrderDetails") Select x.OrderDetails.FirstOrDefault()

"Error translating Linq expression to URI: The method 'Select' is not supported."

From x in context.Order Select x.OrderDetails.FirstOrDefault()

"Error translating Linq expression to URI: The method 'Select' is not supported."

Foi útil?

Solução

It seems to me that aggregating (in your case FirstOrDefault) on navigation properties is not currently supported in OData except for the special cases of all and any. You either need to retrieve all of the OrderDetails (i.e. just do Expand and do a FirstOrDefault on the client) which may or may not be acceptable depending on the amount of data and your performance requirements or create a specific service operation method on the server to retrieve exactly the data you require. It seems that support for aggregation is coming in OData 4. There is a specification dated 26.02.2014 that defines aggregation in OData - http://docs.oasis-open.org/odata/odata-data-aggregation-ext/v4.0/cs01/odata-data-aggregation-ext-v4.0-cs01.html#_Toc378326290 but I don't know if current tools support it yet.

To implement the first approach with expand you need to do something like this

var result = (from order in context.Orders.Expand(o=>o.OrderDetails)
              where...
              orderby...
              select order).Skip(...).Take(...) //if you need paging
              .ToList();

var actualResult = from o in result
                   select new { Order = o, FirstDetail = o.FirstOrDefault() };

Of course depending on your needs you may not get the first detail in this way. The point is that you execute the service query (.ToList()) before dealing with the projection.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top