类型Microsoft.OData.Edm.Date和System.Nullable System.DateTimeOffset

时间:2017-03-13 10:50:50

标签: c# asp.net-mvc controller odata

我们正在建立一个odata网络服务,并面临转换问题。

使用以下odata查询网址:

"http://some.url.ext/test/endPoint?$filter=SomeDate ge 1990-01-01"

在我们的odata过滤枚举中,SomeDate是Nullable<DateTime> DateTime?

然而,看起来odata控制器在Nullable<DateTimeOffset> DateTimeOffset?

中投放了它

然后抱怨它无法比较Microsoft.OData.Edm.DateSystem.Nullable System.DateTimeOffset

错误是:

  

URI中指定的查询无效。没有强制操作员   在类型'Microsoft.OData.Edm.Date'和。之间定义   'System.Nullable`1 [System.DateTimeOffset]'。

No coercion operator is defined between types 'Microsoft.OData.Edm.Date' and 'System.Nullable`1[System.DateTimeOffset]'.

System.InvalidOperationException at System.Linq.Expressions.Expression.GetUserDefinedCoercionOrThrow(ExpressionType coercionType, Expression expression, Type convertToType)
       at System.Linq.Expressions.Expression.Convert(Expression expression, Type type, MethodInfo method)
       at System.Web.OData.Query.Expressions.ExpressionBinderBase.CreateConvertExpression(ConvertNode convertNode, Expression source)
       at System.Web.OData.Query.Expressions.FilterBinder.BindConvertNode(ConvertNode convertNode)
       at System.Web.OData.Query.Expressions.FilterBinder.Bind(QueryNode node)
       at System.Web.OData.Query.Expressions.FilterBinder.BindBinaryOperatorNode(BinaryOperatorNode binaryOperatorNode)
       at System.Web.OData.Query.Expressions.FilterBinder.Bind(QueryNode node)
       at System.Web.OData.Query.Expressions.FilterBinder.BindExpression(SingleValueNode expression, RangeVariable rangeVariable, Type elementType)
       at System.Web.OData.Query.Expressions.FilterBinder.BindFilterClause(FilterBinder binder, FilterClause filterClause, Type filterType)
       at System.Web.OData.Query.Expressions.FilterBinder.Bind(FilterClause filterClause, Type filterType, IServiceProvider requestContainer)
       at System.Web.OData.Query.FilterQueryOption.ApplyTo(IQueryable query, ODataQuerySettings querySettings)
       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, ODataQueryContext queryContext)
       at System.Web.OData.EnableQueryAttribute.OnActionExecuted(HttpActionExecutedContext actionExecutedContext)

使用IEnumerable输出数据调用ODataController.Ok()方法时会触发错误。

我们正在使用Micrtosoft.Odata.Edm 7.0.0

1 个答案:

答案 0 :(得分:4)

通过在过滤器中使用DateTimeOffset日期格式来解决它! 只需使用它如下:

"http://some.url.ext/test/endPoint?$filter=SomeDate ge 1990-01-01T00:00:00Z"

希望这有帮助