title: Dynamic Linq Order By Variable
body: <p>I have a table that I am writing some <code>C#</code> <code>selenium</code> automation for and need some help using Dynamic Linq. Let's say I have a basic <code>AcctNum</code>, <code>AcctDate</code> and <code>AcctName</code> record and each field can be sorted by the user. They could choose <code>AcctNum</code> (asc), <code>AcctDate</code>(asc) and finally <code>AcctName</code> (asc).  </p>
  
  <p>That would be:</p>
  
  <pre><code> var sortedCode = records.OrderBy(r =&gt; r.AcctNum)
                           .ThenBy(r =&gt; r.AcctDate)
                           .ThenBy(r =&gt; r.AcctName)
                           .ToList();
  </code></pre>
  
  <p>However the user could also choose <code>AcctNum</code> (desc), <code>AcctDate</code>(asc) and finally <code>AcctName</code> (desc).  </p>
  
  <p>What I would like to do is use Dynamic Linq and make each sort order a variable.           </p>
  
  <p>So something like:</p>
  
  <pre><code> //passed in values:
   var varAcctNumOrd = "desc";
   var varDateOrd = "asc";
   var varAcctOrd = "desc";
  
   var sortedCode = records.OrderBy(r =&gt; r.AcctNum, varAcctNumOrd)
                           .ThenBy(r =&gt; r.AcctDate, varDateOrd)
                           .ThenBy(r =&gt; r.AcctNum, varAcctOrd)
                           .ToList();
  </code></pre>
  
  <p>Is this possible?</p>