title: Approach To Building Expression Tress with Dynamic Objects
body: <p>I'm looking for help on how to approach an interesting issue.</p>
  
  <p>We have code that will build expression trees (LINQ) to search/sort/paginate a collection of strongly typed objects. The Expression Tree code uses reflection to build up the expression trees and will work an any collection (as long as it's strongly typed).</p>
  
  <pre><code>IEnumerable&lt;T&gt; Parse&lt;T&gt;(Request request, IQueryable&lt;T&gt; queryableData) where T: class
  // Request Contains the searching/sorting/pagination criteria eg: FuzzySearch: "FindAnyObjectWithThis"
  </code></pre>
  
  <p>We recently had the request to be able to customize the fields that where being searched/sorted on. Basically the user could select columns dynamically to include/exclude. To accomplish this, we started by using dynamic objects (ExpandoObject) but this broke the parsing functionality for there was no underlying type for reflection to use.</p>
  
  <ol>
  <li><p>We could change the Parse to use DynamicLinq to parse the collections but that would require us to change the Parse function.</p></li>
  <li><p>We could attempt to dynamically build a type and instantiate the type when retrieving the original data but this could prove to be slow.</p></li>
  <li><p>An approach we haven't thought of.</p></li>
  </ol>
  
  <p>Has anyone else done anything similar to this? How did you approach it?</p>