title: Dynamic linq order by on nested property with null properties
body: <p>I'm using this dynamic linq orderby function which I got from <a href="https://stackoverflow.com/questions/41244/dynamic-linq-orderby-on-ienumerablet">here</a>.</p>
  
  <p>This works fine with nested properties so I could do this:</p>
  
  <pre><code>var result = data.OrderBy("SomeProperty.NestedProperty");
  </code></pre>
  
  <p>The problem is that if SomeProperty is null then performing the OrderBy on the NestedProperty throws the infamous "Object reference not set to an instance of an object".</p>
  
  <p>My guess is that I need to customize the following lines to handle the exception:</p>
  
  <pre><code>expr = Expression.Property(expr, pi);
  
  // Or
  
  LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg);    
  </code></pre>
  
  <p>I thought about creating a statement body where I could in the worst case scenario use a try catch but that didn't work as you can't have statement bodies within orderby linq statements: "A lambda expression with a statement body cannot be converted to an expression tree"</p>
  
  <p>I'm lost over here, any suggestions on how I can accomplish this?</p>
  
  <p><strong>By the way, this is for Linq to Objects, not database related.</strong></p>