0

The following is a great example of how to create an Expression tree when the operator and value is passed into the method as SEPERATE parameters.

Get list on basis of dropdownlist data in asp.net mvc3

In my example i have several dropdownboxes where the operator and value are combined e.g. ">= 1", "< 3" etc. I could potentially split this into operator and value, passing both to the example above but was wondering if there is a simpler way to write the expression where i can just use the operator and value as one parameter, replacing MakeBinary method with an alternative.

I am relatively new to Expression trees so some guidance would be helpful. Thanks.

bob56
  • 29
  • 7

1 Answers1

1

No. Expression trees are quite low level, and don't handle string->code. They aren't eval, they are build code at runtime (technically they are build descriptor for code at runtime, and if you really want, compile it.

Use a regex to split operator and value, if they are in the form "<something".

var rx = new Regex("([<>]=?|==)(.*)");

string str = "<=1234";

var match = rx.Match(str);
string op = match.Groups[1].Value;
string val = match.Groups[2].Value;
xanatos
  • 109,618
  • 12
  • 197
  • 280