title: Dynamic Linq query on a collection of a base type while accessing type specific properties
body: <p>How can I run a dynamic LINQ query on a collection of a base type (like the IPerson interface) but access implementation specific properties (like Age).</p>
  
  <p>I can be sure that all items in the collection are the same, i.e. looking at the first type I can assume that the others are the same.</p>
  
  <p>I need this for a UI that can apply filters to different collections, the user sees all available properties.</p>
  
  <p>Here's an example of what I'd like to do, the Expression.Call method throws an exception:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Linq.Expressions;
  using System.Linq.Dynamic;
  using DynamicExpression = System.Linq.Dynamic.DynamicExpression;
  
  namespace DynamicLinqTest
  {
      public interface IPerson
      {
          string Name { get; set; }
      }
      public class Person : IPerson
      {
          public string Name { get; set; }
          public int Age { get; set; }
          public double Income { get; set; }
      }
  
      class Program
      {
          public static IEnumerable&lt;Person&gt; GetPersons()
          {
              yield return new Person { Name = "Sam", Age = 26, Income = 50000 };
              yield return new Person { Name = "Rick", Age = 27, Income = 0 };
              yield return new Person { Name = "Joe", Age = 45, Income = 35000 };
              yield return new Person { Name = "Bill", Age = 31, Income = 40000 };
              yield return new Person { Name = "Fred", Age = 56, Income = 155000 };
          } 
  
          static void Main(string[] args)
          {
              IEnumerable&lt;IPerson&gt; persons = GetPersons();
              var personsQueriable = persons.AsQueryable();
  
              //what I would like to do:
              // personsQueriable.Where("Age &gt; 30");
  
              var l = DynamicExpression.ParseLambda(persons.First().GetType(), typeof(bool), "Age &gt; 30");
              var filtered = personsQueriable.Provider.CreateQuery(
                  Expression.Call(
                      typeof(Queryable), "Where",
                      new Type[] { persons.First().GetType() },
                      personsQueriable.Expression, Expression.Quote(l)));
  
              ObjectDumper.Write(filtered);
              Console.Read();
          }
      }
  }
  </code></pre>