title: How to construct Order By Expression dynamically in Entity Framework?
body: <p>I used the following methods to construct <strong>Order By Expression</strong>. <a href="https://code.msdn.microsoft.com/AngularJS-with-Web-API-43e5de16/sourcecode?fileId=139277&amp;pathId=481390404" rel="noreferrer">Original Source</a></p>
  
  <p>It is really slick. The downside is it only works if Property is <strong>string</strong> type. </p>
  
  <p>How can I make it to accept different Property type <em>without creating a bunch of methods for different data types</em>? </p>
  
  <pre><code>public static bool PropertyExists&lt;T&gt;(string propertyName)
  {
      return typeof (T).GetProperty(propertyName, BindingFlags.IgnoreCase |
        BindingFlags.Public | BindingFlags.Instance) != null;
  }
  
  public static Expression&lt;Func&lt;T, string&gt;&gt; GetPropertyExpression&lt;T&gt;(string propertyName)
  {
      if (typeof(T).GetProperty(propertyName, BindingFlags.IgnoreCase | 
          BindingFlags.Public | BindingFlags.Instance) == null)
      {
          return null;
      }
  
      var paramterExpression = Expression.Parameter(typeof(T));
  
      return (Expression&lt;Func&lt;T, string&gt;&gt;)Expression.Lambda(
          Expression.PropertyOrField(paramterExpression, propertyName), paramterExpression);
  }
  </code></pre>
  
  <h2>Usage</h2>
  
  <pre><code>// orderBy can be either Name or City.
  if (QueryHelper.PropertyExists&lt;Club&gt;(orderBy)) 
  { 
     var orderByExpression = QueryHelper.GetPropertyExpression&lt;Club&gt;(orderBy); 
     clubQuery = clubQuery.OrderBy(orderByExpression); 
  } 
  else 
  { 
     clubQuery = clubQuery.OrderBy(c =&gt; c.Id); 
  } 
  </code></pre>
  
  <h2>Problem</h2>
  
  <pre><code>public class Club 
  { 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public string City { get; set; } 
    public DateTime CreateDate { get; set; } &lt;= this won't work
  } 
  </code></pre>
  
  <h2>My Current Approach <em>(Too many if statements)</em></h2>
  
  <pre><code>public static Expression&lt;Func&lt;TSource, TKey&gt;&gt; 
      GetPropertyExpression&lt;TSource, TKey&gt;(string propertyName)
  {
      if (typeof (TSource).GetProperty(propertyName, BindingFlags.IgnoreCase | 
          BindingFlags.Public | BindingFlags.Instance) == null)
      {
          return null;
      }
      var paramterExpression = Expression.Parameter(typeof (TSource));
      return (Expression&lt;Func&lt;TSource, TKey&gt;&gt;) 
          Expression.Lambda(Expression.PropertyOrField(
             paramterExpression, propertyName), paramterExpression);
  }
  </code></pre>
  
  <p>The downside is I end up with a lot of if statements for each datatype.</p>
  
  <pre><code>if (QueryHelper.PropertyExists&lt;Club&gt;(orderBy)) 
  {
     if(orderBy == "CreateDate")
     {       
        var orderByExpression = GetPropertyExpression&lt;Club, DateTime&gt;(orderBy);
        ...
     }
     else if(orderBy == "Name" || orderBy == "City")
     {
        var orderByExpression = GetPropertyExpression&lt;Club, string&gt;(orderBy);
        ...
     }
     ...
  }
  else 
  { 
     clubQuery = clubQuery.OrderBy(c =&gt; c.Id); 
  } 
  </code></pre>