简体   繁体   English

将表达式转换为表达式 <Func<T, bool> &gt;

[英]Convert Expression to Expression<Func<T, bool>>

Is that possible to convert Expression to Expression<Func<T, bool>> if instance of Expression was created on T ? 如果在T创建表达式的实例,是否可以将Expression转换为Expression<Func<T, bool>>

At the end I have list List<Expression> and need to produce on Expression<Func<T, bool>> where each expression of List<Expression> is agregated with AND . 最后我有列表List<Expression>并且需要在Expression<Func<T, bool>>上生成List<Expression>其中List<Expression>每个表达式用AND聚合。

It's possible, but every expression in the list must actually be a Expression<Func<T, bool>> instance. 这是可能的,但列表中的每个表达式实际上必须是Expression<Func<T, bool>>实例。

EDIT: It turns out that you use Kendo.Mvc.IFilterDescriptor.CreateFilterExpression which actually builds a MethodCallExpression s. 编辑:事实证明你使用Kendo.Mvc.IFilterDescriptor.CreateFilterExpression实际上构建了一个MethodCallExpression

The following helper method should do the job (works with both lambda and method call expressions): 以下辅助方法应该完成这项工作(适用于lambda和方法调用表达式):

public static class Utils
{
    public static Expression<Func<T, bool>> And<T>(List<Expression> expressions)
    {
        var item = Expression.Parameter(typeof(T), "item");
        var body = expressions[0].GetPredicateExpression(item);
        for (int i = 1; i < expressions.Count; i++)
            body = Expression.AndAlso(body, expressions[i].GetPredicateExpression(item));
        return Expression.Lambda<Func<T, bool>>(body, item);
    }

    static Expression GetPredicateExpression(this Expression target, ParameterExpression parameter)
    {
        var lambda = target as LambdaExpression;
        var body = lambda != null ? lambda.Body : target;
        return new ParameterBinder { value = parameter }.Visit(body);
    }

    class ParameterBinder : ExpressionVisitor
    {
        public ParameterExpression value;
        protected override Expression VisitParameter(ParameterExpression node)
        {
            return node.Type == value.Type ? value : base.VisitParameter(node);
        }
    }
}

Yes; 是; just call Expression.Lambda<Func<T, bool>>(..., parameter) , where ... is an expression composed of the expressions you want to combine. 只需调用Expression.Lambda<Func<T, bool>>(..., parameter) ,其中...是一个由要组合的表达式组成的表达式。

You'd probably want list.Aggregate(Expressions.AndAlso) . 你可能想要list.Aggregate(Expressions.AndAlso)

If your expressions don't all share the same ParameterExpression , you'll need to rewrite them to do so. 如果你的表达式并不都共享同一个ParameterExpression ,那么你需要重写它们才能这样做。 (use ExpressionVisitor ) (使用ExpressionVisitor

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 转换表达式 <Func<T,T,bool> &gt;表达 <Func<T,bool> &gt; - Convert Expression<Func<T,T,bool>> to Expression<Func<T,bool>> 是否可以转换表达式 <Func<T, bool> &gt;表达 <Func<MyType, bool> &gt;? - Is it possible to convert Expression<Func<T, bool>> to Expression<Func<MyType, bool>>? 如何转换Func <T,bool> 表达 <Func<T,bool> &gt; - How to convert Func<T,bool> to Expression<Func<T,bool>> 转换谓词 <T> 表达 <Func<T, bool> &gt; - Convert Predicate<T> to Expression<Func<T, bool>> 通过为T引入一个常量,将表达式<Func <T,T2,bool >>转换为Expression <Func <T2,bool >> - Convert Expression<Func<T, T2, bool>> to Expression<Func<T2, bool>> by introducing a constant for T 转换表达式 <Func<T,bool> &gt;到表达式 <Func<T1,bool> &gt;使T是T1的成员 - Convert an Expression<Func<T,bool>> to an Expression<Func<T1,bool>> so that T is a member of T1 转换表达式 <Func<T1,bool> &gt;表达 <Func<T2,bool> 动态 - Convert Expression<Func<T1,bool>> to Expression<Func<T2,bool> dynamically 在表达式中转换类型<Func<T1, bool> &gt; 表达<Func<T2, bool> &gt; (LINQ to SQL) - Convert type in Expression<Func<T1, bool>> to Expression<Func<T2, bool>> (LINQ to SQL) 如何转换表达式 <Func<T1, bool> &gt;表达 <Func<T2, bool> &gt; - How to convert Expression<Func<T1, bool>> to Expression<Func<T2, bool>> 转换表达式<func<t1, bool> &gt; 到表达式<func<t2, bool> &gt; </func<t2,></func<t1,> - Convert Expression<Func<T1, bool>> to Expression<Func<T2, bool>>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM