title: Telerik MVC grid - ArgugumentException when filtering
body: <p>Greetings,</p>
  
  <p>I'm having the problem posted <a href="http://www.telerik.com/community/forums/aspnet-mvc/grid/the-argument-to-dbisnullexpression-must-refer-to-a-primitive-or-reference-type.aspx" rel="nofollow noreferrer">here</a> (the post dated August 18th is mine), but not getting anywhere.</p>
  
  <p>In a nutshell, filtering with Telerik's MVC grid causes an ArgumentException with the message "The argument to DbIsNullExpression must refer to a primitive or reference type." Interestingly, if you .ToList() the IQueryable first, you avoid the problem (but pulling back all that data isn't a good solution). Also using Entity Framework and Ajax, not sure if that's an issue or not. Here's the stack trace:</p>
  
  <pre><code>System.ArgumentException was unhandled by user code
    Message=The argument to DbIsNullExpression must refer to a primitive or reference type.
    Source=System.Data.Entity
    StackTrace:
         at System.Data.Common.CommandTrees.ExpressionBuilder.Internal.ArgumentValidation.ValidateIsNull(DbExpression argument, Boolean allowRowType)
         at System.Data.Objects.ELinq.ExpressionConverter.EqualsTranslator.CreateIsNullExpression(ExpressionConverter parent, Expression input)
         at System.Data.Objects.ELinq.ExpressionConverter.EqualsTranslator.TypedTranslate(ExpressionConverter parent, BinaryExpression linq)
         at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
         at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
         at System.Data.Objects.ELinq.ExpressionConverter.NotTranslator.TypedTranslate(ExpressionConverter parent, UnaryExpression linq)
         at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
         at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
         at System.Data.Objects.ELinq.ExpressionConverter.NotEqualsTranslator.TypedTranslate(ExpressionConverter parent, BinaryExpression linq)
         at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
         at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
         at System.Data.Objects.ELinq.ExpressionConverter.ConditionalTranslator.TypedTranslate(ExpressionConverter parent, ConditionalExpression linq)
         at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
         at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
         at System.Data.Objects.ELinq.ExpressionConverter.BinaryTranslator.TypedTranslate(ExpressionConverter parent, BinaryExpression linq)
         at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
         at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
         at System.Data.Objects.ELinq.ExpressionConverter.TranslateIntoCanonicalFunction(String functionName, Expression Expression, Expression[] linqArguments)
         at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.CanonicalFunctionDefaultTranslator.Translate(ExpressionConverter parent, MethodCallExpression call)
         at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq)
         at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
         at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
         at System.Data.Objects.ELinq.ExpressionConverter.EqualsTranslator.TypedTranslate(ExpressionConverter parent, BinaryExpression linq)
         at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
         at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
         at System.Data.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input)
         at System.Data.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input, DbExpressionBinding& binding)
         at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, DbExpression& source, DbExpressionBinding& sourceBinding, DbExpression& lambda)
         at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call)
         at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, SequenceMethod sequenceMethod)
         at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq)
         at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
         at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
         at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.AggregateTranslator.Translate(ExpressionConverter parent, MethodCallExpression call)
         at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, SequenceMethod sequenceMethod)
         at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq)
         at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
         at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
         at System.Data.Objects.ELinq.ExpressionConverter.Convert()
         at System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption)
         at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
         at System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable.GetEnumerator()
         at System.Data.Objects.ObjectQuery`1.GetEnumeratorInternal()
         at System.Data.Objects.ObjectQuery.System.Collections.IEnumerable.GetEnumerator()
         at System.Linq.Enumerable.d__b1`1.MoveNext()
         at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
         at System.Data.Objects.ELinq.ObjectQueryProvider.b__3[TResult](IEnumerable`1 sequence)
         at System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot)
         at System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute(Expression expression)
         at Telerik.Web.Mvc.Extensions.QueryableExtensions.Count(IQueryable source) in C:\Projects\SoftExposure\Telerik.Web.Mvc\Extensions\QueryableExtensions.cs:line 399
         at Telerik.Web.Mvc.Extensions.QueryableExtensions.ToGridModel(IQueryable queryable, Int32 page, Int32 pageSize, IList`1 sortDescriptors, IEnumerable`1 filterDescriptors, IEnumerable`1 groupDescriptors) in C:\Projects\SoftExposure\Telerik.Web.Mvc\Extensions\QueryableExtensions.cs:line 45
         at Telerik.Web.Mvc.UI.GridDataProcessor.EnsureDataSourceIsProcessed() in C:\Projects\SoftExposure\Telerik.Web.Mvc\UI\Grid\GridDataProcessor.cs:line 162
         at Telerik.Web.Mvc.UI.GridDataProcessor.get_ProcessedDataSource() in C:\Projects\SoftExposure\Telerik.Web.Mvc\UI\Grid\GridDataProcessor.cs:line 132
         at Telerik.Web.Mvc.GridActionAttribute.OnActionExecuted(ActionExecutedContext filterContext) in C:\Projects\SoftExposure\Telerik.Web.Mvc\UI\Grid\GridActionAttribute.cs:line 104
         at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
         at System.Web.Mvc.ControllerActionInvoker.c__DisplayClassd.c__DisplayClassf.b__c()
         at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
    InnerException: 
  </code></pre>
  
  <p>and here's the code that causes it</p>
  
  <pre><code>        // Note: We need to use a ViewModel rather than the entity directly because the entity relationships
          // will cause a circular reference when used via Ajax.
          // See http://www.telerik.com/help/aspnet-mvc/telerik-ui-components-grid-troubleshooting.html#ServerError
          // 
          private IEnumerable GetCustomers()
          {
              var model = new NorthwindEntities().Customers
                  .Select(c => new CustomerModel()
                  {
                      CustomerID = c.CustomerID,
                      CompanyName = c.CompanyName,
                      ContactName = c.ContactName
                  }); // Call .ToList() here to avoid "The argument to DbIsNullExpression must refer to a primitive or reference type."
                      // when filtering.
                      // See http://www.telerik.com/community/forums/aspnet-mvc/grid/the-argument-to-dbisnullexpression-must-refer-to-a-primitive-or-reference-type.aspx
  
              return model;
          }
  
  </code></pre>
  
  <p>Stepping through the source, it's easy to see that it's happening in the QueryableExtensions.Count() method, but the real reason is because of what's happening in the .Where() method when one has filterDescriptors. Whatever the expression building stuff is doing seems to be the cause.</p>
  
  <p>This is the first time I've looked that deeply into expression trees and what not, if anyone can suggest something to try, it's appreciated.</p>
  
  <p>Regards,
  Mike</p>