0

我正在使用ASP.NET WEB API Odata库。如果我在调用中使用 substringof 并且值为 null 则失败。

我传递的URI是: ... odata/MyEntity()?$filter=substringof(null,Name)...

结果是:

<?xml version="1.0" encoding="utf-8"?>
<m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
  <m:code />
  <m:message xml:lang="en-US">The query specified in the URI is not valid.</m:message>
  <m:innererror>
    <m:message>The 'substringof' function cannot be applied to an enumeration-typed argument.</m:message>
    <m:type>Microsoft.Data.OData.ODataException</m:type>
    <m:stacktrace>   at System.Web.Http.OData.Query.Expressions.FilterBinder.ValidateAllStringArguments(String functionName, Expression[] arguments)&#xD;
   at System.Web.Http.OData.Query.Expressions.FilterBinder.BindSubstringOf(SingleValueFunctionCallNode node)&#xD;
   at System.Web.Http.OData.Query.Expressions.FilterBinder.BindSingleValueFunctionCallNode(SingleValueFunctionCallNode node)&#xD;
   at System.Web.Http.OData.Query.Expressions.FilterBinder.Bind(QueryNode node)&#xD;
   at System.Web.Http.OData.Query.Expressions.FilterBinder.BindFilterClause(FilterClause filterClause, Type filterType)&#xD;
   at System.Web.Http.OData.Query.Expressions.FilterBinder.Bind(FilterClause filterClause, Type filterType, IEdmModel model, IAssembliesResolver assembliesResolver, ODataQuerySettings querySettings)&#xD;
   at System.Web.Http.OData.Query.FilterQueryOption.ApplyTo(IQueryable query, ODataQuerySettings querySettings, IAssembliesResolver assembliesResolver)&#xD;
   at System.Web.Http.OData.Query.ODataQueryOptions.ApplyTo(IQueryable query, ODataQuerySettings querySettings)&#xD;
   at System.Web.Http.QueryableAttribute.ApplyQuery(IQueryable queryable, ODataQueryOptions queryOptions)&#xD;
   at System.Web.Http.QueryableAttribute.ExecuteQuery(IEnumerable query, HttpRequestMessage request, HttpActionDescriptor actionDescriptor)&#xD;
   at System.Web.Http.QueryableAttribute.OnActionExecuted(HttpActionExecutedContext actionExecutedContext)</m:stacktrace>
  </m:innererror>
</m:error>

所以这似乎是 WEB API Odata 库中的一个问题。有什么办法可以预防吗?

4

1 回答 1

1

那应该会失败 - null 永远不会是字符串的子字符串。$filter=substringof(null, Name)在代码中等价于Where(i => i.Name.Contains(null))抛出 ArgumentNullException 之类的东西。

如果要检查值是否为空,可以使用类似的东西$filter=Name eq null,或者检查它是空还是名称的子字符串,可以使用$filter=(Name eq null) or (substringof('theValue', Name)).

于 2013-08-02T22:30:00.187 回答