按日期筛选的 OData 抛出 "The query specified in the URI is not valid. An identifier was expected at position 62"
OData filter by Date throws "The query specified in the URI is not valid. An identifier was expected at position 62"
我正在使用 OData 从我的 SQL Db 查询数据,我正在尝试使用日期值过滤数据。请参阅下面的查询:
http://localhost:4409/OrderMaintenance?$filter=DocType%20eq%205%20and%20DocState%20in%20(0,1,2,3,4,5,6,7)%20and%20InvDate%20eq%202022/02/21
此查询中只有日期是问题,因为当我删除日期过滤器时,查询工作正常。
InvDate 的类型为:
orderDateFilter: Date = new Date();
然后我将日期格式化为:
this.orderDateFilter.toLocaleDateString()
returns 日期为:
2022/02/21
但是我收到以下错误
"The query specified in the URI is not valid. An identifier was expected at position 62."
堆栈跟踪:
at Microsoft.OData.UriParser.ExpressionToken.GetIdentifier()\r\n at Microsoft.OData.UriParser.UriQueryExpressionParser.ParseSegment(QueryToken parent)\r\n at Microsoft.OData.UriParser.UriQueryExpressionParser.ParsePrimary()\r\n at Microsoft.OData.UriParser.UriQueryExpressionParser.ParseInHas()\r\n at Microsoft.OData.UriParser.UriQueryExpressionParser.ParseUnary()\r\n at Microsoft.OData.UriParser.UriQueryExpressionParser.ParseMultiplicative()\r\n at Microsoft.OData.UriParser.UriQueryExpressionParser.ParseAdditive()\r\n at Microsoft.OData.UriParser.UriQueryExpressionParser.ParseComparison()\r\n at Microsoft.OData.UriParser.UriQueryExpressionParser.ParseLogicalAnd()\r\n at Microsoft.OData.UriParser.UriQueryExpressionParser.ParseLogicalOr()\r\n at Microsoft.OData.UriParser.UriQueryExpressionParser.ParseExpression()\r\n at Microsoft.OData.UriParser.UriQueryExpressionParser.ParseExpressionText(String expressionText)\r\n at Microsoft.OData.UriParser.ODataQueryOptionParser.ParseFilterImplementation(String filter, ODataUriParserConfiguration configuration, ODataPathInfo odataPathInfo)\r\n at Microsoft.OData.UriParser.ODataQueryOptionParser.ParseFilter()\r\n at Microsoft.AspNet.OData.Query.FilterQueryOption.get_FilterClause()\r\n at Microsoft.AspNet.OData.Query.Validators.FilterQueryValidator.Validate(FilterQueryOption filterQueryOption, ODataValidationSettings settings)\r\n at Microsoft.AspNet.OData.Query.FilterQueryOption.Validate(ODataValidationSettings validationSettings)\r\n at Microsoft.AspNet.OData.Query.Validators.ODataQueryValidator.Validate(ODataQueryOptions options, ODataValidationSettings validationSettings)\r\n at Microsoft.AspNet.OData.Query.ODataQueryOptions.Validate(ODataValidationSettings validationSettings)\r\n at Microsoft.AspNet.OData.EnableQueryAttribute.ValidateQuery(HttpRequestMessage request, ODataQueryOptions queryOptions)\r\n at Microsoft.AspNet.OData.EnableQueryAttribute.<>c__DisplayClass0_0.<OnActionExecuted>b__1(ODataQueryContext queryContext)\r\n at Microsoft.AspNet.OData.EnableQueryAttribute.ExecuteQuery(Object responseValue, IQueryable singleResultCollection, IWebApiActionDescriptor actionDescriptor, Func`2 modelFunction, IWebApiRequestMessage request, Func`2 createQueryOptionFunction)\r\n at Microsoft.AspNet.OData.EnableQueryAttribute.OnActionExecuted(Object responseValue, IQueryable singleResultCollection, IWebApiActionDescriptor actionDescriptor, IWebApiRequestMessage request, Func`2 modelFunction, Func`2 createQueryOptionFunction, Action`1 createResponseAction, Action`3 createErrorAction)
尝试使用破折号而不是反斜杠来格式化 odata 日期过滤器:
2022-02-21 而不是 2022/02/21。
您可能还需要添加时间部分。这是一个时间为午夜 2022-02-21T00:00:00Z 的示例。
我用过:
this.orderDateFilter.toISOString()
将我的日期转换为:
2022-02-22T09:17:24.822Z
但是我在查询时遇到了问题,因为我只想检查不包括时间的日期。所以我改为使用:
this.datepipe.transform(this.orderDateFilter.toLocaleDateString(),'yyyy-MM-dd')
效果很好!
我正在使用 OData 从我的 SQL Db 查询数据,我正在尝试使用日期值过滤数据。请参阅下面的查询:
http://localhost:4409/OrderMaintenance?$filter=DocType%20eq%205%20and%20DocState%20in%20(0,1,2,3,4,5,6,7)%20and%20InvDate%20eq%202022/02/21
此查询中只有日期是问题,因为当我删除日期过滤器时,查询工作正常。
InvDate 的类型为:
orderDateFilter: Date = new Date();
然后我将日期格式化为:
this.orderDateFilter.toLocaleDateString()
returns 日期为:
2022/02/21
但是我收到以下错误
"The query specified in the URI is not valid. An identifier was expected at position 62."
堆栈跟踪:
at Microsoft.OData.UriParser.ExpressionToken.GetIdentifier()\r\n at Microsoft.OData.UriParser.UriQueryExpressionParser.ParseSegment(QueryToken parent)\r\n at Microsoft.OData.UriParser.UriQueryExpressionParser.ParsePrimary()\r\n at Microsoft.OData.UriParser.UriQueryExpressionParser.ParseInHas()\r\n at Microsoft.OData.UriParser.UriQueryExpressionParser.ParseUnary()\r\n at Microsoft.OData.UriParser.UriQueryExpressionParser.ParseMultiplicative()\r\n at Microsoft.OData.UriParser.UriQueryExpressionParser.ParseAdditive()\r\n at Microsoft.OData.UriParser.UriQueryExpressionParser.ParseComparison()\r\n at Microsoft.OData.UriParser.UriQueryExpressionParser.ParseLogicalAnd()\r\n at Microsoft.OData.UriParser.UriQueryExpressionParser.ParseLogicalOr()\r\n at Microsoft.OData.UriParser.UriQueryExpressionParser.ParseExpression()\r\n at Microsoft.OData.UriParser.UriQueryExpressionParser.ParseExpressionText(String expressionText)\r\n at Microsoft.OData.UriParser.ODataQueryOptionParser.ParseFilterImplementation(String filter, ODataUriParserConfiguration configuration, ODataPathInfo odataPathInfo)\r\n at Microsoft.OData.UriParser.ODataQueryOptionParser.ParseFilter()\r\n at Microsoft.AspNet.OData.Query.FilterQueryOption.get_FilterClause()\r\n at Microsoft.AspNet.OData.Query.Validators.FilterQueryValidator.Validate(FilterQueryOption filterQueryOption, ODataValidationSettings settings)\r\n at Microsoft.AspNet.OData.Query.FilterQueryOption.Validate(ODataValidationSettings validationSettings)\r\n at Microsoft.AspNet.OData.Query.Validators.ODataQueryValidator.Validate(ODataQueryOptions options, ODataValidationSettings validationSettings)\r\n at Microsoft.AspNet.OData.Query.ODataQueryOptions.Validate(ODataValidationSettings validationSettings)\r\n at Microsoft.AspNet.OData.EnableQueryAttribute.ValidateQuery(HttpRequestMessage request, ODataQueryOptions queryOptions)\r\n at Microsoft.AspNet.OData.EnableQueryAttribute.<>c__DisplayClass0_0.<OnActionExecuted>b__1(ODataQueryContext queryContext)\r\n at Microsoft.AspNet.OData.EnableQueryAttribute.ExecuteQuery(Object responseValue, IQueryable singleResultCollection, IWebApiActionDescriptor actionDescriptor, Func`2 modelFunction, IWebApiRequestMessage request, Func`2 createQueryOptionFunction)\r\n at Microsoft.AspNet.OData.EnableQueryAttribute.OnActionExecuted(Object responseValue, IQueryable singleResultCollection, IWebApiActionDescriptor actionDescriptor, IWebApiRequestMessage request, Func`2 modelFunction, Func`2 createQueryOptionFunction, Action`1 createResponseAction, Action`3 createErrorAction)
尝试使用破折号而不是反斜杠来格式化 odata 日期过滤器: 2022-02-21 而不是 2022/02/21。 您可能还需要添加时间部分。这是一个时间为午夜 2022-02-21T00:00:00Z 的示例。
我用过:
this.orderDateFilter.toISOString()
将我的日期转换为:
2022-02-22T09:17:24.822Z
但是我在查询时遇到了问题,因为我只想检查不包括时间的日期。所以我改为使用:
this.datepipe.transform(this.orderDateFilter.toLocaleDateString(),'yyyy-MM-dd')
效果很好!