title: How to perform multiple Linq to Entities orderings dynamically
body: <p>So my problem is like this; in C# code, I need to perform multiple orderings of an entity set with the help of Linq to Entities, dependent on input parameters. There are three columns to order on, and the order of the ordering columns is itself variable (meaning that for each ordering, I need to look up which column to order on). How can I achieve this?</p>
  
  <p>I have some code that should work, but I repeat myself way too much since I haven't been able to parameterize my operations (Linq to Entities is very restrictive wrt. what I'm allowed to do in my lambdas). Please suggest how I can rewrite my code in accordance with the <a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself" rel="nofollow">DRY</a> principle, perhaps with the help of T4 code generation?</p>
  
  <p>The following code should illustrate my problem. It's an excerpt of the real code, for brevity, let me know if I should include more. The <code>orderSpecs</code> variable is an array of "order specifications", each of which specifying a column to order on and whether to order in a descending manner. The <code>orderSpecs</code> array has at least one element, so at least one ordering is performed.</p>
  
  <pre><code>using (var db = new MyContainer())
  {
      var orderSpec = orderSpecs[0];
      IQueryable&lt;DbVersion&gt; dVersions = null;
      if (orderSpec.Column == 0)
      {
          if (orderSpec.Descending)
          {
              dVersions = db.Versions.OrderByDescending(ver =&gt; ver.Name);
          }
          else
          {
              dVersions = db.Versions.OrderBy(ver =&gt; ver.Name);
          }
      }
      else if (orderSpec.Column == 1)
      {
          if (orderSpec.Descending)
          {
              dVersions = db.Versions.OrderByDescending(ver =&gt; ver.Built);
          }
          else
          {
              dVersions = db.Versions.OrderBy(ver =&gt; ver.Built);
          }
      }
      else if (orderSpec.Column == 2)
      {
          if (orderSpec.Descending)
          {
              dVersions = db.Versions.OrderByDescending(ver =&gt; ver.Id);
          }
          else
          {
              dVersions = db.Versions.OrderBy(ver =&gt; ver.Id);
          }
      }
  
      foreach (var spec in orderSpecs.Skip(1))
      {
          if (spec.Column == 0)
          {
              if (spec.Descending)
              {
                  dVersions = dVersions.ThenByDescending(ver =&gt; ver.Name);
              }
              else
              {
                  dVersions = dVersions.ThenBy(ver =&gt; ver.Name);
              }
          }
          else if (spec.Column == 1)
          {
              if (spec.Descending)
              {
                  dVersions = dVersions.ThenByDescending(ver =&gt; ver.Built);
              }
              else
              {
                  dVersions = dVersions.ThenBy(ver =&gt; ver.Built);
              }
          }
          else if (spec.Column == 2)
          {
              if (spec.Descending)
              {
                  dVersions = dVersions.ThenByDescending(ver =&gt; ver.Id);
              }
              else
              {
                  dVersions = dVersions.ThenBy(ver =&gt; ver.Id);
              }
          }
      }
  </code></pre>