Skip to content

GroupBy DateTime properties doesn't work #912

Closed

Description

@isahrnddev6

Grouping by DateTime properties does not work. EnableQuery attribute throws System.ArgumentException.

Assemblies affected

Microsoft.AspNet.OData 5.9.1

Reproduce steps

Sent a request that contains a grouping transformation on an entity for a DateTime property:
http://services.odata.org/V4/Northwind/Northwind.svc/Orders?$apply=groupby((OrderDate))

Expected result

The service should return grouped data.

Actual result

System.ArgumentException is thrown.

Additional details

Activity

  1. TehWardy commented on Apr 4, 2017

    @TehWardy

    "Me too" and I think this may be related to #918

  2. TehWardy commented on Apr 7, 2017

    @TehWardy

    playing with it a bit more I managed to get this exception information from a simple "~/API/Type?$groupby((DateTimeField))" query ...

    {
      "error": {
        "code": "",
        "message": "An error has occurred.",
        "innererror": {
          "message": "Argument types do not match",
          "type": "System.ArgumentException",
          "stacktrace": "   at System.Linq.Expressions.Expression.Bind(MemberInfo member, Expression expression)\r\n   at System.Web.OData.Query.Expressions.AggregationBinder.CreateGroupByMemberAssignments(Type type, IEnumerable`1 properties)\r\n   at System.Web.OData.Query.Expressions.AggregationBinder.BindGroupBy(IQueryable query)\r\n   at System.Web.OData.Query.ApplyQueryOption.ApplyTo(IQueryable query, ODataQuerySettings querySettings, IAssembliesResolver assembliesResolver)\r\n   at System.Web.OData.Query.ODataQueryOptions.ApplyTo(IQueryable query, ODataQuerySettings querySettings)\r\n   at System.Web.OData.EnableQueryAttribute.ApplyQuery(IQueryable queryable, ODataQueryOptions queryOptions)\r\n   at System.Web.OData.EnableQueryAttribute.ExecuteQuery(Object response, HttpRequestMessage request, HttpActionDescriptor actionDescriptor)\r\n   at System.Web.OData.EnableQueryAttribute.OnActionExecuted(HttpActionExecutedContext actionExecutedContext)\r\n   at System.Web.Http.Filters.ActionFilterAttribute.OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Filters.ActionFilterAttribute.<ExecuteActionFilterAsyncCore>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Filters.AuthorizationFilterAttribute.<ExecuteAuthorizationFilterAsyncCore>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"
        }
      }
    }
    

    Here's the stack trace "cleaned up" ...

    System.ArgumentException: Argument types do not match
    	at System.Linq.Expressions.Expression.Bind(MemberInfo member, Expression expression)
    	at System.Web.OData.Query.Expressions.AggregationBinder.CreateGroupByMemberAssignments(Type type, IEnumerable`1 properties)
    	at System.Web.OData.Query.Expressions.AggregationBinder.BindGroupBy(IQueryable query)
    	at System.Web.OData.Query.ApplyQueryOption.ApplyTo(IQueryable query, ODataQuerySettings querySettings, IAssembliesResolver assembliesResolver)
    	at System.Web.OData.Query.ODataQueryOptions.ApplyTo(IQueryable query, ODataQuerySettings querySettings)
    	at System.Web.OData.EnableQueryAttribute.ApplyQuery(IQueryable queryable, ODataQueryOptions queryOptions)
    	at System.Web.OData.EnableQueryAttribute.ExecuteQuery(Object response, HttpRequestMessage request, HttpActionDescriptor actionDescriptor)
    	at System.Web.OData.EnableQueryAttribute.OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
    	at System.Web.Http.Filters.ActionFilterAttribute.OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
    --- End of stack trace from previous location where exception was thrown ---
    	at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    	at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    	at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
    	at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    	at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    	at System.Web.Http.Filters.ActionFilterAttribute.<ExecuteActionFilterAsyncCore>d__0.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
    	at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    	at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    	at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
    	at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    	at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    	at System.Web.Http.Filters.AuthorizationFilterAttribute.<ExecuteAuthorizationFilterAsyncCore>d__2.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
    	at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    	at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    	at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()
    
  3. hbdbim commented on May 31, 2017

    @hbdbim

    +1

  4. rcreynolds53 commented on Jul 27, 2018

    @rcreynolds53

    I am still experiencing issues when trying to groupby a DateTime, is there a work around for this or has it been resolved?

  5. TehWardy commented on Aug 20, 2018

    @TehWardy

    so it's been a good 18 months ... any movement on this @mtroth-microsoft ?

  6. sc-starman commented on Dec 28, 2018

    @sc-starman

    I'm encountered this problem by so far...
    Is there any workaround that you can suggest?

  7. hbdbim commented on Dec 28, 2018

    @hbdbim

    I'm encountered this problem by so far...
    Is there any workaround that you can suggest?

    Yes, make a string property that represents your date and groups by it.

  8. linked a pull request that will close this issuegroup by on date throws exceptions #2302on Oct 7, 2020
  9. TehWardy commented on Oct 14, 2020

    @TehWardy

    @ElizabethOkerio did you check that this works for DateTimeOffset properties too (just highlighting a somewhat obvious escalation).

  10. ElizabethOkerio commented on Oct 14, 2020

    @ElizabethOkerio
    Contributor

    @TehWardy yes this works for DateTimeOffset properties too. Have you had issues with DateTimeOffset properties? If so, please share.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions