title: LINQ: Sort a list depending on parameter
body: <p>I have a LINQ to object query to select all the persons that are above 20 years old</p>
  
  <pre><code>IEnumerable&lt;Object&gt; result = null;
  result = (from person in AllPersons.ToList()
            where  person.age &gt; 20
                    select new
                    {
                        FirstName= person.FirstName,
                        LastName= person.LastName,
                        Email= person.Email,
                        PhoneNumber= person.PhoneNumber
  
  
                    });
  return result;
  </code></pre>
  
  <p>I have a parameter <code>string SortProperty</code> I want to use to sort the result based on the property. </p>
  
  <p>So for example if <code>SortProperty="FirstName"</code> I want to sort the result based on the first name.</p>
  
  <p>I tried to do the following:</p>
  
  <pre><code>return result.OrderBy(x =&gt; x.GetType().GetProperty(SortProperty));
  </code></pre>
  
  <p>but it did not work</p>
  
  <p>any idea how to do it?</p>
  
  <p>PS: I don't want to test all the possibilities, and do a if-else on each, or a case switch. I'm looking for an efficient way to do this</p>
  
  <p>Thanks </p>