title: Creating Sorting Func<IQueryable<T>, IOrderedQueryable<T>>?
body: <p>I wanna create a sorting generic Func <code>Creating Func&lt;IQueryable&lt;T&gt;, IOrderedQueryable&lt;T&gt;&gt;</code> in an extension method :</p>
  
  <pre><code>public static Func&lt;IQueryable&lt;T&gt;, IOrderedQueryable&lt;T&gt;&gt; GetOrderByFunc&lt;T&gt;(this KeyValuePair&lt;string, SortingType&gt; keyValuePair)
  {
      //I expect the following result
      //Func&lt;IQueryable&lt;T&gt;, IOrderedQueryable&lt;T&gt;&gt; orderby = q =&gt; q.OrderByDescending(c =&gt; c.Name);
  
      //keyValuePair.Key is name of property in type of T that I should sort T by it
      switch (keyValuePair.Value)
      {
          case SortingType.Ascending:
              // Creating Ascending Sorting Func
              break;
  
          case SortingType.Descending:
              // Creating Descending Sorting Func
              break;
  
          default :
              break;
      }
  }
  </code></pre>
  
  <p>Could you guide me, how I can do it?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>this sorting also, contains of count of a T's navigation property.<br />
  e.g:</p>
  
  <pre><code>// keyValuePair.Key equals "User.Count"
  // User is a navigation property of T
  Func&lt;IQueryable&lt;T&gt;, IOrderedQueryable&lt;T&gt;&gt; orderby = q =&gt; q.OrderByDescending(c =&gt; c.User.Count);
  </code></pre>
  
  <p><strong>Edit:</strong></p>
  
  <p>I changed <code>GetSelector</code> as the following, but an exception has occurred in <code>bodyExpression</code>.</p>
  
  <pre><code>public static Expression GetSelector&lt;T&gt;(string propertyName)
  {
      ParameterExpression parameter = Expression.Parameter(typeof(T));
      if (propertyName.Contains("."))
      {
          propertyName = propertyName.Substring(0, propertyName.IndexOf("."));
          Type navigationPropertyCollectionType = typeof(T).GetProperty(propertyName).PropertyType;
  
          if (navigationPropertyCollectionType.GetGenericTypeDefinition() == typeof(ICollection&lt;&gt;))
          {                        
              Expression countParameter = Expression.Parameter(navigationPropertyCollectionType, "c");
              MemberExpression countExpression = Expression.Property(countParameter, "Count");
  //Exception: Instance property 'Users(ICollection`1)' is not defined for type 'System.Int32'
              var bodyExpression = Expression.Property(countExpression, propertyName, countParameter);
              return Expression.Lambda(bodyExpression, parameter);
          }
      }
      MemberExpression bodyMemberExpression = Expression.Property(parameter, typeof(T).GetProperty(propertyName));
      return Expression.Lambda(bodyMemberExpression, parameter);
  } 
  </code></pre>