c# - Use lambda expression in another lambda expression -
I have to add two lambda expressions, in this way the second expression is included in "first" I searched a lot but I No clear answer was found ...
What I am trying to do is the following: An expression "expression 1" is passed as a parameter for a method, and its use only . To define which area or property will be the second Lambda work
As a drawing, I am trying to do the following:
// Simple area Selector: expression & lt; & Lt; T, string & gt; & Gt; Expression1 = obj = & gt; Obj.field; // Try using the Field Selector in the second expression: Expression & lt; Func & lt; T, bool & gt; & Gt; E2 = obj = & gt; [[Result of expression 1]]. Equal ("homewhich");
In other words, I would like to receive:
Expression & lt; Funk & lt; T, bool & gt; & Gt; E2 = obj = & gt; Obj.field.Equals ("myValue");
I need to do this because it is not always the similarity ()
method that will be called, but many different methods.
function & lt expression1
in implementing this function in sequence expression2, but as i was using this with linq, i have an exception because LinQ invoke Does not support the node type. So my question is: is there a way to combine the body of two types of expressions, and how please?
Thanks in advance!
OK, this should do the trick:
void main ) {Var execute = Create & lt; Test class & gt; (First = & gt; first.moststrillingfield, // field selector second = & gt; second; ecloss ("1234") / method selector); Console (light) (execute new test class ("1234"); // true console. Light line (execute (new test class ("4321"); // false} class test class {public string mystringfield ; Public Test Class (String Val) {MyStringField = val;}} Fixed Funk & lt; TSource, bool & gt; Create & lt; TSource & gt; (& lt; TSource; String & gt; ; & Gt; fieldSelector; & lt; string; bool & gt; method of expression; methodSelector) {// TODO: rule of fieldSelector If necessary, the classical validation of the method selection, if necessary, var compiledFieldSelector = fieldSelector.Compile (); var compiledMethodSelector = methodSelector.Compile (); Return T => compiledMethodSelector (compiledFieldSelector (T) );
Note, due to the nature of lambda bhav, you need to validate the field selector and method selector, otherwise it is possible to do some very weird work.
Alternatively, the next approach is Lambda that says "creation" things, No Unable to add makes it later than LinqToEntities query interpretation should not be a problem
static function. & Lt; TSource, bool & gt; Create & LT; TSource & gt; (& Lt; TSource, string & gt; & gt; memberSelector of the function & lt; function & lt; function & lt; string; bool & gt; & gt; methodSelector of function; // TODO: memberSelector Classical recognition, if necessary. // todo: classical verification of method selection, if necessary Var Member Expression = (Member Exercise) (Member selector.bd); Var Vidalolx = (Methodclassification) (method selector.bd); // Input TSource = & gt; .. var input = expression Parameter (typef (tssor)); Var call = expression Call (Expression. Meckmember Entrance (Input, Member Feedback.member), VitalolXpress. Method, ViatrolXpressAgargram); Return expression. Lambda & lt; Func & lt; TSource, bool & gt; (Call, input) .compile (); }
Comments
Post a Comment