c# - Dynamically Linq Select Cast To IEnumerable -
i'm creating widget builder dynamically takes in queries , returns datatable results. note: uses dynamic linq take in string queries library source can found here
my issue casting results set ienumerable.
public datatable getentitydata<d>(string query, int numbofresults, list<string> columns) d : class { objectcontext objectcontext = ((iobjectcontextadapter)this).objectcontext; var fdw = (objectcontext.createobjectset<d>() iqueryable<d>).where(query).take(numbofresults); string column = "new(id, exttitleid)"; var res = fdw.select(column).cast<object>().tolist(); homecoming datatablecaster.createtableobj(res); }
this effort cast @ line
var res = fdw.select(column).cast<object>().tolist();
i error "unable cast type 'dynamicclass1' type 'system.object'. linq entities supports casting edm primitive or enumeration types."
it must anonymous type can grab entity related properties cannot cast list of string using reflection ie
// cannot grab right properties var fd = p in fdw.tolist() select ( ( col in columns select p.gettype().getproperty(col).getvalue(p, null).tostring() ).tolist() ).tolist();
the code below unable subproperties of internal types.
i overrided dynamic.cs class suppor ienumerable select instead of iqueryable back upwards cast. override class utilize code
public static ienumerable<t> select<t>(this ienumerable source, string selector, params object[] values) { homecoming select(source, selector, values).cast<t>(); } public static ienumerable select(this ienumerable source, string selector, params object[] values) { if (source == null) throw new argumentnullexception("source"); if (selector == null) throw new argumentnullexception("selector"); lambdaexpression lambda = dynamicexpression.parselambda(source.asqueryable().elementtype, null, selector, values); homecoming source.asqueryable().provider.createquery( expression.call( typeof(queryable), "select", new type[] { source.asqueryable().elementtype, lambda.body.type }, source.asqueryable().expression, expression.quote(lambda))); }
c# linq reflection dynamic-linq
No comments:
Post a Comment