title: How can I use linq to sort by multiple fields?
body: <p>I'm creating a mock data source that I want to be able to pass in a list of SortExpressions on.</p>
  
  <pre><code>public SortExpression(string name, SortDirection direction)
  {
       this.name = name;
       this.direction = direction;
  }
  </code></pre>
  
  <p><strong>Update</strong> with Jon Skeet's code and also the entire class.  GetData() is just populating the object with x number of records.</p>
  
  <pre><code>public class Data
  {
  
  public int Id { get; set; }
  public Guid gId { get; set; }
  public string Name { get; set; }
  public string Phone { get; set; }
  public string Address { get; set; }
  public DateTime Created { get; set; }
  public string SortMe { get; set; }
  public static List&lt;Data&gt; GetFakeData(int start, int numberToFetch, IList&lt;SortExpression&gt; sortExpressions, IList&lt;FilterExpression&gt; filterExpressions, out int totalRecords)
  {
      DataCollection items = GetData();
      IEnumerable&lt;Data&gt; query = from item in items select item;
  
      bool sortExpressionsExist = sortExpressions != null;
      if (sortExpressionsExist)
      {
          // Won't be read in the first iteration; will be written to
          IOrderedEnumerable&lt;Data&gt; orderedQuery = null;
          for (int i = 0; i &lt; sortExpressions.Count; i++)
          {
              // Avoid single variable being captured: capture one per iteration.
              // Evil bug which would be really hard to find :)
              int copyOfI = i;
              // Tailor "object" depending on what GetProperty returns.
              Func&lt;Data, object&gt; expression = item =&gt;
                    item.GetType().GetProperty(sortExpressions[copyOfI].Name);
  
              if (sortExpressions[i].Direction == SortDirection.Ascending)
              {
                  orderedQuery = (i == 0) ? query.OrderBy(expression)
                                          : orderedQuery.ThenBy(expression);
              }
              else
              {
                  orderedQuery = (i == 0) ? query.OrderByDescending(expression)
                                          : orderedQuery.ThenByDescending(expression);
              }
          }
          query = orderedQuery;
      }
  
      bool filterExpressionsExist = filterExpressions != null;
      if (filterExpressionsExist)
      {
          foreach (var filterExpression in filterExpressions)
          {
              query.Where(item =&gt; item.GetType().GetProperty(filterExpression.ColumnName).GetValue(item, null).ToString().Contains(filterExpression.Text));
          }
      }
      totalRecords = query.Count();
  
  
         return query.Skip(start).Take(numberToFetch).ToList&lt;Data&gt;();
      }
  }
  </code></pre>
  
  <p>Doesn't seem to be doing anything.  Compiles, no errors, just no sort.  Any ideas?</p>