using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Linq.Expressions;

namespace Lambda05
{
    public class Program
    {
        public static void Main(string[] args)
        {
            int val = 4;

            // Ausdrucksbaum auslesen
            Expression<Func<int, bool>> exp = p => p < 7;

            BinaryExpression body = (BinaryExpression) exp.Body;
            ParameterExpression left = (ParameterExpression) body.Left; // p
            ConstantExpression right = (ConstantExpression) body.Right; // 7
            Console.WriteLine("{0} {1} {2}", left.Name, body.NodeType, right.Value);

            if (exp.Compile()(val))
                Console.WriteLine("kleiner 7");
            else
                Console.WriteLine("groesser oder gleich 7");

            // Ausdrucksbaum erzeugen
            ParameterExpression parameterExpression = Expression.Parameter(typeof(int), "n");
            ConstantExpression constExpression = Expression.Constant(7, typeof(int));
            BinaryExpression lessThan = Expression.LessThan(parameterExpression, constExpression);
            Expression<Func<int, bool>> lambdaExpression = Expression.Lambda<Func<int, bool>>(lessThan, new ParameterExpression[] { parameterExpression });

            if (lambdaExpression.Compile()(val))
                Console.WriteLine("kleiner 7");
            else
                Console.WriteLine("groesser oder gleich 7");
            
            Console.ReadLine();
        }
    }
}