c#-类型Microsoft.OData.Edm.Date和System.Nullable System.DateTimeOffset之间没有定义强制运算符

我们正在构建一个odata Web服务,并且面临转换问题.

使用以下odata查询网址:

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

在我们的经过odata过滤的枚举中,SomeDate是Nullable< DateTime>.约会时间?.

然而,看起来odata控制器将其强制转换为Nullable< DateTimeOffset>. DateTimeOffset?

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

错误是:

The query specified in the URI is not valid. No coercion operator is
defined between types ‘Microsoft.OData.Edm.Date’ and
‘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

解决方法:

好了,通过在过滤器中使用DateTimeOffset日期格式解决了它!
只需按以下方式使用它:

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

希望这可以帮助

上一篇:c#-通过Web API 2将ASP.NET身份(用户管理)添加到OData


下一篇:如何在OData中按嵌套属性过滤?