c# - Create Expression for List.Any clause -
consider hierarchy of classes.
class event { public attendees[] attendeeslist; } class attendees { public complexproperty property; public object value; } class complexproperty { } class program { static void main(string[] args) { // there constants. complexproperty constproperty = new complexproperty(); object constvalue = 5; // consider linq query: event evnt = new event(); var result = evnt.attendeeslist.any((attnds) => attnds.property == constproperty && attnds.value == constvalue); // want create expression tree above linq query. need this: parameterexpression parameter = expression.parameter(typeof(attendees)); expression left = expression.property(parameter, typeof(complexproperty).getproperty("property")); // complete piece....... } }
i want create linq.expressions.expression
evnt.attendeeslist.any((attnds) => attnds.property == constproperty && attnds.value == constvalue);
linq query. how query collection linq.expressions looks similar, have any
in linq expression.
please help.
this give start:
parameterexpression parameter = expression.parameter(typeof(attendees)); expression left = expression.equal(expression.property(parameter, "property"), expression.constant(constproperty)); var objtype = constvalue == null ? typeof(object) : constvalue.gettype(); var convertleft = expression.convert(expression.property(parameter, "value"), objtype); var convertright = expression.convert(expression.constant(constvalue), objtype); expression right = expression.equal(convertleft, convertright); expression joined = expression.and(left, right); var anymethod = typeof(queryable).getmethods().where(m => m.name=="any" && m.getparameters().count() == 2).first().makegenericmethod(typeof(attendees)); var call = expression.call(anymethod, expression.constant(evnt.attendeeslist, typeof(iqueryable<attendees>)), expression.lambda(joined, parameter));
i changed value type int
ease of use. if want keep object, need add expression.convert() call in right
expression.
Comments
Post a Comment