title: Extending Marc Gravell's Dynamic Linq OrderBy
body: <p>I found Marc Gravell's dynamic order by great:</p>
  
  <p><a href="https://stackoverflow.com/questions/41244/dynamic-linq-orderby">Dynamic LINQ OrderBy on IEnumerable&lt;T&gt;</a></p>
  
  <p>I've put it in a class, <code>LinqHelper</code>.  In this class I also have created two new classes, so that in my code I can do this:</p>
  
  <pre><code>var q = db.tblJobHeaders;
  
  LinqHelper.OrderByCollection OBys = new LinqHelper.OrderByCollection();
  OBys.AddOrderBy("some field", true);
  OBys.AddOrderBy("anotherfield", false);
  OBys.ExecuteOrderBys(q);
  </code></pre>
  
  <p>The classes to acheive this are:</p>
  
  <pre><code>/// &lt;summary&gt;
  /// A collection of order bys
  /// &lt;/summary&gt;
  public class OrderByCollection
  {
      private ArrayList Orderings = new ArrayList();
  
      public OrderByCollection(){ }
  
      /// &lt;summary&gt;
      /// Add an order by to this collection
      /// &lt;/summary&gt;
      public void AddOrderBy(string Field, bool Descending)
      {
          OrderByObj NewObj = new OrderByObj(Descending, Field);
          this.Orderings.Add(NewObj);
      }
  
      /// &lt;summary&gt;
      /// Executes the order bys
      /// &lt;/summary&gt;
      public IOrderedQueryable&lt;T&gt; ExecuteOrderBys&lt;T&gt;(this IOrderedQueryable&lt;T&gt; source)
      {
          int ExecutionIndex = 0;
          foreach (OrderByObj O in this.Orderings)
          {
              if (ExecutionIndex == 0)
              {
                  if (O.Descending)
                      source = LinqHelper.OrderByDescending(source, O.Field);
                  else
                      source = LinqHelper.OrderBy(source, O.Field);
              }
              else
              {
                  if (O.Descending)
                      source = LinqHelper.ThenByDescending(source, O.Field);
                  else
                      source = LinqHelper.ThenBy(source, O.Field);
              }
              ExecutionIndex++;
          }
          return (IOrderedQueryable&lt;T&gt;)source;
      }
  }
  
  /// &lt;summary&gt;
  /// An order by object
  /// &lt;/summary&gt;
  private class OrderByObj
  {
      public bool Descending { get; set; }
      public string Field { get; set; }
  
      public OrderByObj(bool IsDescending, string DatabaseField)
      {
          this.Descending = IsDescending;
          this.Field = DatabaseField;
      }
  }
  </code></pre>
  
  <p>Howver I'm pretty new to passing Linq vars through to functions (the  confuses me a bit).  I currently get the error on:</p>
  
  <pre><code>OBys.ExecuteOrderBys(q);
  </code></pre>
  
  <p>Which gives the error:</p>
  
  <blockquote>
    <p>The type arguments for method
    'LinqHelper.OrderByCollection.ExecuteOrderBys(System.Linq.IOrderedQueryable)'
    cannot be inferred from the usage. Try
    specifying the type arguments
    explicitly.</p>
  </blockquote>
  
  <p>I'm a bit confused about this if anyone could help, am I passing the <code>var q</code> in properly, and then returning it properly?</p>