title: C# - code to order by a property using the property name as a string
body: <p>What's the simplest way to code against a property in C# when I have the property name as a string?  For example, I want to allow the user to order some search results by a property of their choice (using LINQ).  They will choose the &quot;order by&quot; property in the UI - as a string value of course.  Is there a way to use that string directly as a property of the linq query, without having to use conditional logic (if/else, switch) to map the strings to properties.  Reflection?</p>
  <p>Logically, this is what I'd like to do:</p>
  <pre><code>query = query.OrderBy(x =&gt; x.&quot;ProductId&quot;);
  </code></pre>
  <p><strong>Update:</strong>
  I did not originally specify that I'm using <code>Linq to Entities</code> - it appears that reflection (at least the GetProperty, GetValue approach) does not translate to L2E.</p>