表达树转换

时间:2010-08-12 11:18:18

标签: c# lambda expression-trees

我有两个课程:

class Customer
{
    public string Fullname { get; set; }
    public string Lastname { get; set; }
    public int Age { get; set; }
}

class CustomerDTO
{
    public string Fullname { get; set; }
    public string Lastname { get; set; }
    public int Age { get; set; }
}

现在我有一个表达式Expression<Func<Customer, bool>> expression在层之间传递,我可以将它转换为Expression<Func<CustomerDTO, bool>> expression以便能够使用它,因为它会给编译时错误!

提前致谢

1 个答案:

答案 0 :(得分:0)

没关系,我做到了

Expression<Func<Customer, bool>> expression = v => v.Fullname == "Johm";
         var par = Expression.Parameter(typeof(CustomerDTO));
         Expression<Func<CustomerDTO, bool>> ex = (Expression<Func<CustomerDTO, bool>>)Expression.Lambda(expression.Body, par);