我正在使用 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)
at System.Web.Http.OData.Query.Expressions.FilterBinder.BindSubstringOf(SingleValueFunctionCallNode node)
at System.Web.Http.OData.Query.Expressions.FilterBinder.BindSingleValueFunctionCallNode(SingleValueFunctionCallNode node)
at System.Web.Http.OData.Query.Expressions.FilterBinder.Bind(QueryNode node)
at System.Web.Http.OData.Query.Expressions.FilterBinder.BindFilterClause(FilterClause filterClause, Type filterType)
at System.Web.Http.OData.Query.Expressions.FilterBinder.Bind(FilterClause filterClause, Type filterType, IEdmModel model, IAssembliesResolver assembliesResolver, ODataQuerySettings querySettings)
at System.Web.Http.OData.Query.FilterQueryOption.ApplyTo(IQueryable query, ODataQuerySettings querySettings, IAssembliesResolver assembliesResolver)
at System.Web.Http.OData.Query.ODataQueryOptions.ApplyTo(IQueryable query, ODataQuerySettings querySettings)
at System.Web.Http.QueryableAttribute.ApplyQuery(IQueryable queryable, ODataQueryOptions queryOptions)
at System.Web.Http.QueryableAttribute.ExecuteQuery(IEnumerable query, HttpRequestMessage request, HttpActionDescriptor actionDescriptor)
at System.Web.Http.QueryableAttribute.OnActionExecuted(HttpActionExecutedContext actionExecutedContext)</m:stacktrace>
</m:innererror>
</m:error>
因此,它似乎是WEB API Odata库中的一个问题。有什么办法可以阻止吗?
答案 0 :(得分:1)
应该失败 - null永远不会是字符串的子串。 $filter=substringof(null, Name)
在代码中等同于Where(i => i.Name.Contains(null))
,它会抛出ArgumentNullException。
如果要检查值是否为null,可以使用$filter=Name eq null
之类的内容,或者检查它是否为null或名称的子字符串,可以$filter=(Name eq null) or (substringof('theValue', Name))
。