OData Web Api Date Filter 'Edm.DateTime' and 'Edm.String' incompatible types

I was getting the following error when trying to filter an odata source by date:

{
    "Message":"The query specified in the URI is not valid.",
    "ExceptionMessage":"A binary operator with incompatible types was detected. Found operand types 'Edm.DateTime' and 'Edm.String' for operator kind 'GreaterThan'.",
    "ExceptionType":"Microsoft.Data.OData.ODataException",
    "StackTrace":"   at Microsoft.Data.OData.Query.BinaryOperatorBinder.PromoteOperandTypes(BinaryOperatorKind binaryOperatorKind, SingleValueNode& left, SingleValueNode& right)
       at Microsoft.Data.OData.Query.BinaryOperatorBinder.BindBinaryOperator(BinaryOperatorQueryToken binaryOperatorToken)
        at Microsoft.Data.OData.Query.MetadataBinder.BindBinaryOperator(BinaryOperatorQueryToken binaryOperatorToken)
         at Microsoft.Data.OData.Query.MetadataBinder.Bind(QueryToken token)
          at Microsoft.Data.OData.Query.FilterBinder.BindFilter(CollectionNode path, QueryToken filter)
           at Microsoft.Data.OData.Query.ODataUriParser.ParseFilter(String filter, IEdmModel model, IEdmType elementType, IEdmEntitySet entitySet)
            at System.Web.Http.OData.Query.FilterQueryOption.get_FilterClause()
             at System.Web.Http.OData.Query.Validators.FilterQueryValidator.Validate(FilterQueryOption filterQueryOption, ODataValidationSettings settings)
              at System.Web.Http.OData.Query.FilterQueryOption.Validate(ODataValidationSettings validationSettings)
               at System.Web.Http.OData.Query.Validators.ODataQueryValidator.Validate(ODataQueryOptions options, ODataValidationSettings validationSettings)
                at System.Web.Http.OData.Query.ODataQueryOptions.Validate(ODataValidationSettings validationSettings)
                 at System.Web.Http.QueryableAttribute.ValidateQuery(HttpRequestMessage request, ODataQueryOptions queryOptions)
                  at System.Web.Http.QueryableAttribute.ExecuteQuery(IEnumerable query, HttpRequestMessage request, HttpActionDescriptor actionDescriptor)
                   at System.Web.Http.QueryableAttribute.OnActionExecuted(HttpActionExecutedContext actionExecutedContext)"}

I was using the following URI:

/api/Trip/Get?%24inlinecount=allpages&%24top=20&%24orderby=Severity&%24filter=Departure+gt+'2013-08-07T12:41:00'

The problem is that OData is interpreting 2013-08-07T12:41:00 as a string and not a datetime as the field was specified on the server.

The solution:

prefix the string with DATETIME like so:

/api/Trip/Get?%24inlinecount=allpages&%24top=20&%24orderby=Severity&%24filter=Departure+gt+DATETIME'2013-08-07T12:41:00'

comments powered by Disqus