Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am using ASP.NET WEB API Odata libraries. If I use substringof in my call and the value is null it fails.

The URI I am passing is: ... odata/MyEntity()?$filter=substringof(null,Name) ...

And the result is:

<?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>

So it seems it is an issue in the WEB API Odata libraries. Is there any way to prevent it?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

That is supposed to fail - null will never be a substring of a string. $filter=substringof(null, Name) is equivalent in code to something like Where(i => i.Name.Contains(null)), which throws an ArgumentNullException.

If you want to check whether the value is null, you can use something like $filter=Name eq null, or to check whether it's null or a substring of the name, you can do $filter=(Name eq null) or (substringof('theValue', Name)).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.