The 1st solution was not what was asked for. 2nd attempt: ;-)
See
http://msdn.microsoft.com/en-us/library/bb397951.aspx[
^] as starting point.
static readonly List<int> data = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
static IEnumerable<int> SelectData(Expression<Predicate<int>> selector)
{
ParameterExpression param = (ParameterExpression)selector.Parameters[0];
BinaryExpression operation = (BinaryExpression)selector.Body;
ParameterExpression left = (ParameterExpression)operation.Left;
ConstantExpression right = (ConstantExpression)operation.Right;
Console.WriteLine("Decomposed expression: {0} => {1} {2} {3}",
param.Name, left.Name, operation.NodeType, right.Value);
return from d in data where selector.Compile()(d) select d;
}
static void Main(string[] args)
{
Console.WriteLine("data = {0}", string.Join(",", SelectData(d=>d>4)));
}
The resulting output is:
Decomposed expression: d => d GreaterThan 4
data = 5,6,7,8,9
You have to make the parsing of the expression far more sophisticated. In the example above, the assumption is that there is exactly one binary operation as body of the lambda expression.
Cheers
Andi