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 here
My only issue is casting the results set to an IEnumerable.
public DataTable GetEntityData<D>(string Query, int NumbOfResults, List<string> Columns)
where D : class
{
ObjectContext objectContext = ((IObjectContextAdapter)this).ObjectContext;
var FDW = (objectContext.CreateObjectSet<D>() as IQueryable<D>).Where(Query).Take(NumbOfResults);
string Column = "new(ID, ExtTitleID)";
var res = FDW.Select(Column).Cast<object>().ToList();
return DataTableCaster.CreateTableObj(res);
}
This is an attempt to cast at line
var res = FDW.Select(Column).Cast<object>().ToList();
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."
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
// 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();
the code below is unable to get subproperties of internal types.
I overrided the Dynamic.cs Class to suppor Ienumerable select instead of Iqueryable which now will support my cast. To Override the class use this code