2013-08-02 73 views
0

我正在使用ASP.NET WEB API Odata庫。如果我在我的調用中使用substringof,並且值爲null,則失敗。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庫的問題。有什麼辦法可以預防它嗎?

回答

1

這應該是失敗 - null永遠不會是一個字符串的子字符串。 $filter=substringof(null, Name)在代碼上與Where(i => i.Name.Contains(null))等效,它會引發ArgumentNullException。

如果要檢查值是否爲空,可以使用類似$filter=Name eq null的東西,或者檢查它是否爲null或名稱的子字符串,則可以執行$filter=(Name eq null) or (substringof('theValue', Name))