title: Is this LINQ dynamic orderby method thread safe?
body: <p>After searching online and going through past stackoverflow posts for a suitable implementation for dynamic ordering with linq, i came up with my own implementation that <a href="http://www.singingeels.com/Blogs/Nullable/2008/03/26/Dynamic_LINQ_OrderBy_using_String_Names.aspx" rel="nofollow noreferrer">borrows a few things</a> from other solutions i have previously seen. </p>
  
  <p>What i need to know is if this implementation is threadsafe? I don't believe it is as i am passing an enumerable generic type object (reference type) as a parameter into the static method but i would like to know if i am missing anything else and hopefully how to make it completely threadsafe.</p>
  
  <pre><code>public class OrderByHelper
  {
  
      public static IEnumerable&lt;T&gt; OrderBy&lt;T&gt;(IQueryable&lt;T&gt; items, string sortColumn, string sortDirection, int pageNumber, int pageSize)
      {
          Type t = typeof(T).GetProperty(sortColumn).PropertyType;
          return (IEnumerable&lt;T&gt;)(typeof(OrderByHelper)
                          .GetMethod("OrderByKnownType")
                          .MakeGenericMethod(new[] { typeof(T), t })
                          .Invoke(null, new object[] { items, sortColumn, sortDirection, pageNumber, pageSize }));
      }
  
      public static IEnumerable&lt;K&gt; OrderByKnownType&lt;K, T&gt;(IQueryable&lt;K&gt; items, string sortColumn, string sortDirection, int pageNumber, int pageSize)
      {
          var param = Expression.Parameter(typeof(K), "i");
          var mySortExpression = Expression.Lambda&lt;Func&lt;K, T&gt;&gt;(Expression.Property(param, sortColumn), param);
  
          if (!string.IsNullOrEmpty(sortDirection))
          {
              if (sortDirection == "ASC")
                  return items.OrderBy(mySortExpression).Skip((pageNumber - 1) * pageSize).Take(pageSize);
              else
                  return items.OrderByDescending(mySortExpression).Skip((pageNumber - 1) * pageSize).Take(pageSize);
          }
          else
              throw new InvalidOperationException("No sorting direction specified.");
      }
  }
  </code></pre>