Sorting by LocalDate field via OData

452 views
Skip to first unread message

hav...@gmail.com

unread,
Jul 26, 2013, 11:39:48 AM7/26/13
to noda...@googlegroups.com
I've run into an issue with trying to sort my data coming from an OData service when the '$orderby' field is a 'LocalDate' type.  I'm able to execute a LINQ DB query with a LocalDate field in an OrderBy statement, no problem, so the problem appears to be with the OData handling of the request itself, not DB related (RavenDB in this case).  The error I'm getting is:

message: The query specified in the URI is not valid
exceptionMessage: The $orderby expression must evaluate to a single value of primitive type.
exceptionType: Microsoft.Data.OData.ODataException
stackTrace: at Microsoft.Data.OData.Query.OrderByBinder.ProcessSingleOrderBy(BindingState state, OrderByClause thenBy, OrderByToken orderByToken)
     at Microsoft.Data.OData.Query.OrderByBinder.BindOrderBy(BindingState state, IEnumerable`1 orderByTokens)
     at Microsoft.Data.OData.Query.ODataUriParser.ParseOrderByImplementation(String orderBy, IEdmType elementType, IEdmEntitySet entitySet)
     at Microsoft.Data.OData.Query.ODataUriParser.ParseOrderBy(String orderBy, IEdmType elementType, IEdmEntitySet entitySet)
     at Microsoft.Data.OData.Query.ODataUriParser.ParseOrderBy(String orderBy, IEdmModel model, IEdmType elementType)
     at System.Web.Http.OData.Query.OrderByQueryOption.get_OrderByClause()
     at System.Web.Http.OData.Query.OrderByQueryOption.get_OrderByNodes()
     at System.Web.Http.OData.Query.ODataQueryOptions.EnsureStableSortOrderBy(OrderByQueryOption orderBy, ODataQueryContext context)
     at System.Web.Http.OData.Query.ODataQueryOptions.ApplyTo(IQueryable query, ODataQuerySettings querySettings)
     at System.Web.Http.QueryableAttribute.ApplyQuery(IQueryable queryable, ODataQueryOptions queryOptions)
     at System.Web.Http.QueryableAttribute.ExecuteQuery(IEnumerable query, HttpRequestMessage request, HttpActionDescriptor actionDescriptor)
     at System.Web.Http.QueryableAttribute.OnActionExecuted(HttpActionExecutedContext actionExecutedContext)

My current workaround requires converting the LocalDate field to a string, which is certainly not ideal.  Is there some workaround to be able to issue an $orderby command via OData on a LocalDate type field or is it really locked down to only allow primitive types (string, decimal, date, etc)?

Matt Johnson

unread,
Jul 26, 2013, 12:11:41 PM7/26/13
to noda...@googlegroups.com, hav...@gmail.com
Are you using https://github.com/mj1856/RavenDB-NodaTime  or something else?

If so, can you provide a failing test and open an issue there?

Thanks,
Matt

hav...@gmail.com

unread,
Jul 26, 2013, 4:42:00 PM7/26/13
to noda...@googlegroups.com, hav...@gmail.com
I was using a portion of that framework (mainly the json serialization bit, which I manually created a project for and imported the code), however testing using the full blown project results in the same error.  I'll work on getting a sample project created and attach it to a new issue.

Matt Johnson

unread,
Jul 26, 2013, 7:23:29 PM7/26/13
to noda...@googlegroups.com, hav...@gmail.com
At this point, you should just be able to use the Nuget package.  I'm assuming you are on RavenDB 2.5 now.  You won't have much success trying parts with 2.0.

The tests for LocalDate are here:

It looks like I'm testing equality and range filtering, so sorting should work also.  But I'm not currently testing that specifically.  I'll go add a few tests with ordering and see if they pass.

I doubt OData has anything to do with it.  Once they get past your front end, it's just like any other query to raven.

Matt Johnson

unread,
Jul 26, 2013, 9:40:35 PM7/26/13
to noda...@googlegroups.com, hav...@gmail.com
I added sorting to all of the queries in the unit tests.  It uncovered issues with Offset, LocalTime and Duration, which are all persisted in RavenDB as if they were TimeSpan types.  I will investigate further.

LocalDate had no problems with sorting.

hav...@gmail.com

unread,
Jul 27, 2013, 12:57:55 AM7/27/13
to noda...@googlegroups.com, hav...@gmail.com
I agree, the problem doesn't appear to be with the sorting of LocalDate properties in RavenDB, but rather something with OData sorting using the '$orderby' directive.  I'm able to sort LocalDate data in RavenDB, no problem, using standard LINQ queries.  I'm also able to perform OData sorts using primitive property types (string, datetime, etc).  The problem seems to be specifically with the OData interface support of non-primitive data types, which prevents the request from being passed on to the database by throwing an exception up front.  I've created an issue with a sample project for testing the functionality.

Matt Johnson

unread,
Jul 27, 2013, 1:40:12 AM7/27/13
to noda...@googlegroups.com
Yes, exactly. The issue is is that Microsoft.Data.Edm only knows about the built in primitive types. You may want to look into extending it for NodaTime types, if it is extensible. I didn't dig in enough to OData to know if that is possible.

On a personal note, I got on the OData bandwagon for a short time, but quickly found it to be more trouble than its worth. I tend to stay away from it these days.

You may find this of interest:
http://stackoverflow.com/a/9579090/634824

Matt Johnson

unread,
Aug 4, 2013, 4:44:14 PM8/4/13
to noda...@googlegroups.com, hav...@gmail.com
FYI - The issues I uncovered here are now resolved.
Reply all
Reply to author
Forward
0 new messages