c# - Using the Select method for dynamic queries and expression trees -
i attempting create dynamic query using look trees match next statement:
var items = data.where(i => i.coveragetype == 2).select(i => i.limitselected);
i can create method , result it; however, cannot create select method.
here method:
var parm = expression.parameter(typeof(baseclassdata), "basecoverage"); var querydata = data.asqueryable(); var left = expression.property(parm, "coveragetype"); var right = expression.constant(2m); var e1 = expression.equal(left, right); var wheremethod = expression.call( typeof(queryable), "where", new type[] { querydata.elementtype }, querydata.expression, expression.lambda<func<baseclassdata, bool>>(e1, new parameterexpression[] { parm }));
this using select method:
var selectparm = expression.property(parm, "limitselected"); var selectmethod = expression.call( typeof(enumerable), "select", new type[]{typeof(baseclassdata), typeof(decimal)}, wheremethod, expression.lambda<func<baseclassdata, decimal>>(selectparm, new parameterexpression[]{ parm}) );
when run code error:
no generic method 'select' on type 'system.linq.enumerable' compatible supplied type arguments , arguments. no type arguments should provided if method non-generic.
i have tried changing enumerable queryable , same error.
no need utilize expression.call, can straight build look tree instead; have create static method help me generate dynamic query:
public static void test(string[] args) { using (var db = new dbcontext()) { //query 1 var query1 = db.prizetypes.where(m => m.rewards == 1000).select(t => t.name); //query 2 equal query 1 expression<func<prizetype, bool>> predicate1 = m => m.rewards == 1000; expression<func<prizetype, string>> selector1 = t => t.name; var query2 = db.prizetypes.where(predicate1).select(selector1); console.writeline(predicate1); console.writeline(selector1); console.writeline(); //query 3 equal query 1 , 2 expression<func<prizetype, bool>> predicate2 = getpredicateequal<prizetype>("rewards", (int16)1000); expression<func<prizetype, string>> selector2 = getselector<prizetype, string>("name"); var query3 = db.prizetypes.where(predicate2).select(selector2); console.writeline(predicate2); console.writeline(selector2); //as can see, query 1 equal query 2 equal query 3 } } public static expression<func<tentity, bool>> getpredicateequal<tentity>(string fieldname, object fieldvalue) tentity : class { parameterexpression m = expression.parameter(typeof(tentity), "t"); var p = m.type.getproperty(fieldname); binaryexpression body = expression.equal( expression.property(m, fieldname), expression.constant(fieldvalue, p.propertytype) ); homecoming expression.lambda<func<tentity, bool>>(body, m); } public static expression<func<t, treturn>> getselector<t, treturn>(string fieldname) t : class treturn : class { var t = typeof(treturn); parameterexpression p = expression.parameter(typeof(t), "t"); var body = expression.property(p, fieldname); homecoming expression.lambda<func<t, treturn>>(body, new parameterexpression[] { p }); }
c# linq exception-handling expression-trees
No comments:
Post a Comment