查询具有可为空 datetimetimeoffset 的属性的链接实体时出现问题

Problems when querying linked entities with properties that are nullable datetimetimeoffset

我正在使用 simple.odata.client v4 访问我的 odata rest api。数据模型相当复杂。我遇到的问题是我只想获取相关实体满足条件的实体,该条件涉及 属性 可为空的 datetimeoffset (OnHandLastUpdated)。实际上在 api 方面,它是一个可为空的日期时间,但我认为 odata v4 会自动转换它。我尝试 运行 的代码是:

        var items =
oDataClient.For<ClientProductSku>()
    .Filter(x => x.ClientId == clientId && x.Product.SupplierProductSkuClient
        .All(y => y.SupplierProductSku.SupplierProductSkuOnHand
            .Any(z => z.OnHandLastUpdated.HasValue && z.OnHandLastUpdated.Value > DateTimeOffset.Now.AddMinutes(-5))))
    .Expand(UpdateSkuOnhandExpandTables)
    .FindEntriesAsync(annotations)
    .Result;

我得到的错误如下:

  "error":{
    "code":"","message":"The query specified in the URI is not valid. Can only bind segments that are Navigation, Structural, Complex, or Collections. We found a segment 'OnHandLastUpdated' that isn't any of those. Please revise the query.","innererror":{
      "message":"Can only bind segments that are Navigation, Structural, Complex, or Collections. We found a segment 'OnHandLastUpdated' that isn't any of those. Please revise the query.","type":"Microsoft.OData.Core.ODataException","stacktrace":"   at Microsoft.OData.Core.UriParser.Parsers.InnerPathTokenBinder.BindInnerPathSegment(InnerPathToken segmentToken)\r\n   at Microsoft.OData.Core.UriParser.Parsers.MetadataBinder.BindInnerPathSegment(InnerPathToken token)\r\n   at Microsoft.OData.Core.UriParser.Parsers.MetadataBinder.Bind(QueryToken token)\r\n   at Microsoft.OData.Core.UriParser.Parsers.EndPathBinder.DetermineParentNode(EndPathToken segmentToken)\r\n   at Microsoft.OData.Core.UriParser.Parsers.EndPathBinder.BindEndPath(EndPathToken endPathToken)\r\n   at Microsoft.OData.Core.UriParser.Parsers.MetadataBinder.BindEndPath(EndPathToken endPathToken)\r\n   at Microsoft.OData.Core.UriParser.Parsers.MetadataBinder.Bind(QueryToken token)\r\n   at Microsoft.OData.Core.UriParser.Parsers.BinaryOperatorBinder.GetOperandFromToken(BinaryOperatorKind operatorKind, QueryToken queryToken)\r\n   at Microsoft.OData.Core.UriParser.Parsers.BinaryOperatorBinder.BindBinaryOperator(BinaryOperatorToken binaryOperatorToken)\r\n   at Microsoft.OData.Core.UriParser.Parsers.MetadataBinder.BindBinaryOperator(BinaryOperatorToken binaryOperatorToken)\r\n   at Microsoft.OData.Core.UriParser.Parsers.MetadataBinder.Bind(QueryToken token)\r\n   at Microsoft.OData.Core.UriParser.Parsers.LambdaBinder.BindExpressionToken(QueryToken queryToken)\r\n   at Microsoft.OData.Core.UriParser.Parsers.LambdaBinder.BindLambdaToken(LambdaToken lambdaToken, BindingState state)\r\n   at Microsoft.OData.Core.UriParser.Parsers.MetadataBinder.BindAnyAll(LambdaToken lambdaToken)\r\n   at Microsoft.OData.Core.UriParser.Parsers.MetadataBinder.Bind(QueryToken token)\r\n   at Microsoft.OData.Core.UriParser.Parsers.LambdaBinder.BindExpressionToken(QueryToken queryToken)\r\n   at Microsoft.OData.Core.UriParser.Parsers.LambdaBinder.BindLambdaToken(LambdaToken lambdaToken, BindingState state)\r\n   at Microsoft.OData.Core.UriParser.Parsers.MetadataBinder.BindAnyAll(LambdaToken lambdaToken)\r\n   at Microsoft.OData.Core.UriParser.Parsers.MetadataBinder.Bind(QueryToken token)\r\n   at Microsoft.OData.Core.UriParser.Parsers.BinaryOperatorBinder.GetOperandFromToken(BinaryOperatorKind operatorKind, QueryToken queryToken)\r\n   at Microsoft.OData.Core.UriParser.Parsers.BinaryOperatorBinder.BindBinaryOperator(BinaryOperatorToken binaryOperatorToken)\r\n   at Microsoft.OData.Core.UriParser.Parsers.MetadataBinder.BindBinaryOperator(BinaryOperatorToken binaryOperatorToken)\r\n   at Microsoft.OData.Core.UriParser.Parsers.MetadataBinder.Bind(QueryToken token)\r\n   at Microsoft.OData.Core.UriParser.Parsers.FilterBinder.BindFilter(QueryToken filter)\r\n   at Microsoft.OData.Core.UriParser.ODataQueryOptionParser.ParseFilterImplementation(String filter, ODataUriParserConfiguration configuration, IEdmType elementType, IEdmNavigationSource navigationSource)\r\n   at Microsoft.OData.Core.UriParser.ODataQueryOptionParser.ParseFilter()\r\n   at System.Web.OData.Query.FilterQueryOption.get_FilterClause()\r\n   at System.Web.OData.Query.Validators.FilterQueryValidator.Validate(FilterQueryOption filterQueryOption, ODataValidationSettings settings)\r\n   at System.Web.OData.Query.FilterQueryOption.Validate(ODataValidationSettings validationSettings)\r\n   at System.Web.OData.Query.Validators.ODataQueryValidator.Validate(ODataQueryOptions options, ODataValidationSettings validationSettings)\r\n   at System.Web.OData.Query.ODataQueryOptions.Validate(ODataValidationSettings validationSettings)\r\n   at System.Web.OData.EnableQueryAttribute.ValidateQuery(HttpRequestMessage request, 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)"
    }

我在某处读到,odata v4 尚未完全支持可为 null 的 datetime 和 datetimeoffset,这可能会导致此问题。是否有解决方法来实现上述查询?

我认为这是 Simple.OData.Client 的当前限制。它有一个内置的表达式解析器,其工作方式与其他 LINQ 提供程序类似。它将 C# 表达式转换为 OData URI。发生的事情是它将 HasValue 视为 OnHandLastUpdated 的 属性,即 OnHandLastUpdated 被解释为 ComplexType,因此您会收到此错误。

我将在 Simple.OData.Client GitHub 注册一个问题,看看是否可以轻松解决。

我检查了库,看起来你可以像这样重写你的查询:

     var items =
oDataClient.For<ClientProductSku>()
    .Filter(x => x.ClientId == clientId && x.Product.SupplierProductSkuClient
        .All(y => y.SupplierProductSku.SupplierProductSkuOnHand
            .Any(z => z.OnHandLastUpdated.Value > DateTimeOffset.Now.AddMinutes(-5))))
    .Expand(UpdateSkuOnhandExpandTables)
    .FindEntriesAsync(annotations)
    .Result;

你能试试吗?我相信它应该有效。

FWIW,我在手动创建表达式树时遇到了同样的问题。我决定四处寻找并尝试一些东西,并借助@Vagif Abilov 的回答将问题缩小到明确使用 HasValue 属性.

如果您 null 而不是 Nullable<DateTimeOffset>.HasValue 比较它有效,并且您不必捏造任何依赖于可空测试或为 OData 框架做一个特例。

因此按照上面的例子你应该可以做到:

 var items = oDataClient.For<ClientProductSku>()
    .Filter(x => x.ClientId == clientId && x.Product.SupplierProductSkuClient
    .All(y => y.SupplierProductSku.SupplierProductSkuOnHand
        .Any(z => z.OnHandLastUpdated != null && 
            z.OnHandLastUpdated.Value > DateTimeOffset.Now.AddMinutes(-5))))
    .Expand(UpdateSkuOnhandExpandTables)
    .FindEntriesAsync(annotations)
    .Result;