title: In C#, how can i add a null check into dynamic express code
body: <p>i am leveraging <a href="http://www.codeproject.com/KB/aspnet/AspNetMVCandJqGrid.aspx" rel="nofollow">this project to use jqgrid to filter and sort collections</a>.  The one issue i see if that, when looping through the collection and checking the property of a string field is null, this code below will blow up.  i want to tweak the code below so it creates a lambda that not only does a "ToString()" and "Contains" but also support if a property is null (and treats it as string.empty)</p>
  
  <p>I basically have 2 inputs into this function below:</p>
  
  <ol>
  <li>FieldName</li>
  <li>Data Value</li>
  </ol>
  
  <p>and the code below is dynamically building up an expression to loop through a collection and check "Contains" against the property that maps onto the FieldName being passed in.</p>
  
  <p>i have code that looks like this:</p>
  
  <pre><code>        ParameterExpression parameter = Expression.Parameter(query.ElementType, "p");
  
          MemberExpression memberAccess = null;
          MethodCallExpression memberAccessToString = null;
          foreach (var property in column.Split('.'))
          {
              memberAccess = MemberExpression.Property(memberAccess ?? (parameter as Expression), property);
              memberAccessToString = MemberExpression.Call(memberAccess, "ToString", new Type[] {}, new Expression[] {});
          }
  
          Expression condition = null;
          LambdaExpression lambda = null;
              case WhereOperation.Contains:
                  condition = Expression.Call(memberAccessToString,
                      typeof(string).GetMethod("Contains"),
                      Expression.Constant(value));
                  lambda = Expression.Lambda(condition, parameter);
  
          MethodCallExpression result = Expression.Call(
                 typeof(Queryable), "Where",
                 new[] { query.ElementType },
                 query.Expression,
                 lambda);
  
          return query.Provider.CreateQuery&lt;T&gt;(result);
  </code></pre>