11

The use case is the following: retrieve the 5 latest posts.

I am trying to get OData results sorted by a Custom Component Metadata date field, but the statement below breaks.

var pres = service.Components.OrderBy(c => c.CustomMetas[0].DateValue);

The error thrown is the following:

Error translating Linq expression to URI: The expression [10007].CustomMetas.get_Item[0].DateValue is not supported.

Obviously I can always sort the data after retrieving the results, however this shouldn't be done for this use case since there could be thousands of results and we would need to parse them, this is far from efficient.

---- EDIT ----

Thanks to Nickoli I got to get it working.

The information I am retrieving matches the following criteria:

  • Components
  • Filter by schema 468
  • 5 entries
  • Results sorted descending based on Custom Meta date field (ReleaseDate)

The URI looks like this:

http://domain/odata.svc/CustomMetas/?$expand=Component/ComponentPresentations&$filter=KeyName%20eq%20%27ReleaseDate%27&&%20Component/SchemaId%20=%20468%20&$orderby=DateValue%20desc%20&$top=5
  • Thanks N! That is great but I think in this case there is an important difference between Meta vs Custom Meta. The custom meta is a different entity while regular meta fields are just properties of the Component entity, so I am not sure we could get it in the same way, experimenting time! – Asier Fernández Apr 11 '13 at 9:18
10

There are several ways to get to an item with oData. For example, a component presentation can be retrieved by entering through the /ComponentPresentations entity, but you can also get to it via the /Component entity with the $expand option, or via the CustomMetas entity via the $expand option. You also have the options to $filter and $orderby, both standard OData protocol commands. See the standard API here: http://www.odata.org/documentation/odata-v2-documentation/uri-conventions/.

So to sort results by CustomMeta you would need to run the following query:

http://<yourODataServiceUrl>/odata.svc/CustomMetas?$expand=Component/ComponentPresentations&$filter=KeyName eq 'YourDateFieldName'&$orderby=StringValue

As great as all the bells and whistles that .NET and Visual Studio offer, it's sometimes analogous to the "park assist" feature in new cars. It makes us forget how to park the car the old fashioned way and we end up backing up into walls when the feature stops functioning to expectations. My point is, figure out how to make LINQ autogenerate this kind of a REST URI and you'll be set.

  • Thank you so much Nick! You got me on the right track, I just needed to added a couple of little details to make it fit my needs! – Asier Fernández Apr 11 '13 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.