title: Create expression from string (null reference)
body: <p>Here is the problem:</p>
  
  <p>We use table object to allow the users to perform some functionality like search, sort, pagination etc. Those tables works great. But there is a problem with one of the functionality :
  The sort (=OrderBy).</p>
  
  <p>In fact, to allow the sort, we set in every column a string that represent the expression:
  For example, if the expression is Person => Person.Id, then the string is Id;
  if the expression is Person => Person.Address.Street, the string is Address.Street.</p>
  
  <p>In the first case (Person => Person.Id), it works great since it is not a sub object.
  But in the second case (Person => Person.Address.Street), it doesn't since the Address object could be null.</p>
  
  <p>To allow the Orderby to be performed from a string, I found on an other post the following methods :</p>
  
  <pre><code>public static IOrderedQueryable&lt;T&gt; OrderBy&lt;T&gt;(this IQueryable&lt;T&gt; source, string property)
  {
      return ApplyOrder&lt;T&gt;(source, property, "OrderBy");
  }
  
  public static IOrderedQueryable&lt;T&gt; OrderByDescending&lt;T&gt;(this IQueryable&lt;T&gt; source, string property)
  {
      return ApplyOrder&lt;T&gt;(source, property, "OrderByDescending");
  }
  
  public static IOrderedQueryable&lt;T&gt; ThenBy&lt;T&gt;(this IOrderedQueryable&lt;T&gt; source, string property)
  {
      return ApplyOrder&lt;T&gt;(source, property, "ThenBy");
  }
  
  public static IOrderedQueryable&lt;T&gt; ThenByDescending&lt;T&gt;(this IOrderedQueryable&lt;T&gt; source, string property)
  {
      return ApplyOrder&lt;T&gt;(source, property, "ThenByDescending");
  }
  
  private static IOrderedQueryable&lt;T&gt; ApplyOrder&lt;T&gt;(IQueryable&lt;T&gt; source, string property, string methodName)
  {
      string[] props = property.Split('.');
      Type type = typeof(T);
      ParameterExpression arg = Expression.Parameter(type, "x");
      Expression expr = arg;
      foreach (string prop in props)
      {
          // use reflection (not ComponentModel) to mirror LINQ 
          PropertyInfo pi = type.GetProperty(prop);
          expr = Expression.Property(expr, pi);
          type = pi.PropertyType;
      }
      Type delegateType = typeof(Func&lt;,&gt;).MakeGenericType(typeof(T), type);
      LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg);
      object result = typeof(Queryable).GetMethods().Single(
      method =&gt; method.Name == methodName
          &amp;&amp; method.IsGenericMethodDefinition
          &amp;&amp; method.GetGenericArguments().Length == 2
          &amp;&amp; method.GetParameters().Length == 2)
              .MakeGenericMethod(typeof(T), type)
              .Invoke(null, new object[] { source, lambda });
      return (IOrderedQueryable&lt;T&gt;)result;
  }
  </code></pre>
  
  <p>Does any of you have an idea that would allow me to add a condition that would not select the object with subobject == null ? Or prevent it from Trying to access a property from an object that is null ?</p>
  
  <p>EDIT :</p>
  
  <p>The check would look something like : list.OrderBy(x => (x.Address != null) ? x.Address.Street : string.Empty).</p>
  
  <p>So I need to add a null check on every object between x and the final field.
  Is it possible to do that using those methods ?</p>
  
  <p>EDIT 2 : </p>
  
  <p>I've tried to replace </p>
  
  <pre><code>Expression.Property(expr, pi);
  </code></pre>
  
  <p>by</p>
  
  <pre><code>expr = Expression.Condition(
                  Expression.Equal(expr, Expression.Constant(null)),
                  Expression.Constant(String.Empty),
                  Expression.Property(expr, pi));
  </code></pre>
  
  <p>But it seems it doesn't work. I get the following exception :</p>
  
  <pre><code>Argument types do not match
  </code></pre>
  
  <p>Any idea how I'm supposed to know the default value for the field accessed by expr ?</p>