c# - Create Expression<Func<TEntity,object>> dynamically and call Aggregate to IQueryable<TEntity> -
i'd create expression<func<tentity, object>>
later pass function parameter.
if use following function creating it...
public expression<func<tentity, object>> getinclude(string property) { parameterexpression parameter = system.linq.expressions.expression.parameter(typeof(tentity)); system.linq.expressions.expression ppty = system.linq.expressions.expression.property(parameter, property); lambdaexpression lambda = system.linq.expressions.expression.lambda(ppty, parameter); return (expression<func<tentity, object>>)lambda; }
... error @ runtime trying return result, saying type cannot converted object. example, if tentity
purchases
, want purchases.customers
giving "customers"
parameter.
on other hand, if function become generic like...
public expression<func<tentity, tdest>> getinclude<tdest>(string property) tdest: class, new() { parameterexpression parameter = system.linq.expressions.expression.parameter(typeof(tentity)); system.linq.expressions.expression ppty = system.linq.expressions.expression.property(parameter, property); lambdaexpression lambda = system.linq.expressions.expression.lambda(ppty, parameter); return (expression<func<tentity, tdest>>)lambda; }
... , calling generic method...
methodinfo methodinfoinclude = entityrepository.getmethod("getinclude"); object[] parameterstoincludearray = new object[] { "customers" }; //for testing //get type of object obtained: assembly data = assembly.load("domain.entities"); type entitytypereferenced = data.gettypes() .where(t => t.isclass && t.namespace == "domain.entities" && t.name == "customers") //for testing .tolist<type>().first(); methodinfo methodinfogenericinclude = methodinfoinclude.makegenericmethod(entitytypereferenced); object include = methodinfogenericinclude.invoke(entityrepositoryinstance, parameterstoincludearray);
... have no error , can include object. however, when call method requires expression<func<tentity, object>>
parameter:
object[] includes = { include }; methodinfo methodinfo = entityrepository.getmethod("getelementswithinclude"); object[] parameterstoincludefunctionarray = new object[] { includes }; object data = methodinfo.invoke(entityrepositoryinstance, parameterstoincludefunctionarray);
... like...
public list<tentity> getelementswithinclude(expression<func<tentity,object>>[] includes) { return this.applyincludes(this._context.createdbset<tentity>(), includes).asenumerable<tentity>().tolist(); }
... the error object[]
cannot converted expression<func<tentity,object>>[]
.
the aim calling function applyincludes
this, having created expressions dynamically:
private iqueryable<tentity> applyincludes(idbset<tentity> entities, expression<func<tentity, object>>[] includes) { return includes.aggregate(entities.asqueryable<tentity>(), (entity, include) => entity.include( ((memberexpression)include.body).member.declaringtype.equals(typeof(tentity)) ? ((memberexpression)include.body).member.name : include.body.tostring().replace(include.parameters[0].name + ".", "").replace("firstordefault().", "") )); }
any this, please???
thank much.
Comments
Post a Comment