title: Dynamically Linq Select Cast To IEnumerable
body: <p>I'm creating a Widget builder which Dynamically takes in Queries and returns a datatable with the results. NOTE: this uses Dynamic Linq to take in string queries the library source can be found <a href="http://weblogs.asp.net/scottgu/dynamic-linq-part-1-using-the-linq-dynamic-query-library" rel="nofollow">here</a></p>
  
  <p>My only issue is casting the results set to an IEnumerable.  </p>
  
  <pre><code>    public DataTable GetEntityData&lt;D&gt;(string Query, int NumbOfResults, List&lt;string&gt; Columns)
         where D : class
      {
  
          ObjectContext objectContext = ((IObjectContextAdapter)this).ObjectContext;
  
          var FDW = (objectContext.CreateObjectSet&lt;D&gt;() as IQueryable&lt;D&gt;).Where(Query).Take(NumbOfResults);
  
          string Column = "new(ID, ExtTitleID)";
  
          var res = FDW.Select(Column).Cast&lt;object&gt;().ToList();            
  
          return DataTableCaster.CreateTableObj(res);
  
      }
  </code></pre>
  
  <p>This is an attempt to cast at line </p>
  
  <pre><code>    var res = FDW.Select(Column).Cast&lt;object&gt;().ToList();   
  </code></pre>
  
  <p>I get The error "Unable to cast the type 'DynamicClass1' to type 'System.Object'. LINQ to Entities only supports casting EDM primitive or enumeration types."</p>
  
  <p>It must be an Anonymous Type so I can grab entity related properties I CANNOT cast this to a list of string using reflection ie</p>
  
  <pre><code>         // I cannot grab the correct Properties with this
           var FD = from p in FDW.ToList()
                   select
                   (
                     (
                      from col in Columns
                      select p.GetType().GetProperty(col).GetValue(p, null).ToString()
                     ).ToList()
                   ).ToList();
  </code></pre>
  
  <p>the code below is unable to get subproperties of internal types.</p>