关于表达式树

我这篇写的应该比脑袋好。

class Program
{
    static void Main(string[] args)
    {
        Test();
    }

    //简单类型 int
    public static void Test()
    {
        List<int> list = new List<int>() { 1, 2, 3, 4 };

        ParameterExpression parameter = Expression.Parameter(typeof(int), "p");

        ExpressionType op = ExpressionType.Equal;
        var para2 = Expression.Constant(1);
        var result2 = Expression.MakeBinary(op, parameter, para2);

        var expression = Expression.Lambda(result2, parameter) as Expression<Func<int, bool>>;

        var newList = list.Where(expression.Compile());
    }

    //复杂类型
    public static void Test1()
    {
        List<ClassA> list = new List<ClassA>();
        list.Add(new ClassA() { A1 = 1, A2 = "2" });
        list.Add(new ClassA() { A1 = 11, A2 = "22" });

        ParameterExpression parameter = Expression.Parameter(typeof(ClassA), "p");

        var left = Expression.PropertyOrField(parameter, "A1");
        var right = Expression.Constant(1);
        ExpressionType op = ExpressionType.GreaterThan;

        var result = Expression.MakeBinary(op, left, right);

        var expression = Expression.Lambda(result, parameter) as Expression<Func<ClassA, bool>>;

        var newList = list.Where(expression.Compile());
    }

    //拼接2个条件
    public static void Test2()
    {
        List<ClassA> list = new List<ClassA>();
        list.Add(new ClassA() { A1 = 1, A2 = "2" });
        list.Add(new ClassA() { A1 = 11, A2 = "22" });
        list.Add(new ClassA() { A1 = 122, A2 = "122" });

        ParameterExpression parameter = Expression.Parameter(typeof(ClassA), "p");

        var left = Expression.PropertyOrField(parameter, "A1");
        var right = Expression.Constant(1);
        ExpressionType op = ExpressionType.GreaterThan;

        var result = Expression.MakeBinary(op, left, right);


        right = Expression.Constant(100);
        op = ExpressionType.LessThan;

        var result2 = Expression.MakeBinary(op, left, right);

        ExpressionType et = ExpressionType.And;
        var newResult = Expression.MakeBinary(et, result, result2);

        var expression = Expression.Lambda(newResult, parameter) as Expression<Func<ClassA, bool>>;

        var newList = list.Where(expression.Compile());
    }

    //调用自定义方法
    public static void Test3()
    {
        List<ClassA> list = new List<ClassA>();
        list.Add(new ClassA() { A1 = 11, A2 = "2" });
        list.Add(new ClassA() { A1 = 11, A2 = "22" });
        list.Add(new ClassA() { A1 = 122, A2 = "122" });

        ParameterExpression parameter = Expression.Parameter(typeof(ClassA), "p");

        var para1 = Expression.PropertyOrField(parameter, "A2");

        MethodCallExpression expCall = Expression.Call(parameter,
            typeof(ClassA).GetMethod("TestMethod"), para1);

        ExpressionType op = ExpressionType.Equal;
        var para2 = Expression.Constant(true);
        var result2 = Expression.MakeBinary(op, expCall, para2);

        var expression = Expression.Lambda(result2, parameter) as Expression<Func<ClassA, bool>>;

        var newList = list.Where(expression.Compile());
    }
}    

public class ClassA
{
    public int A1 { get; set; }
    public string A2 { get; set; }

    public bool TestMethod(string pa)
    {
        return pa == "22";
    }
}
posted @ 2010-03-15 19:17  包建强  Views(1612)  Comments(5Edit  收藏  举报