结合两个Linq表达式

时间:2012-11-22 12:50:44

标签: c# linq

使用来自各个SO帖子的信息,特别是此blog(更正为使用AndAlso而不是And)我已设法将类似类型的linq表达式组合到单个谓词中。但现在我想结合两个表达式,其中一个是另一个的输入。这是完全展开的原始Expression;

    private Expression<Func<T, bool>> ExpressionIsNamed(IEnumerable<EntityName> AccessorNames)
    {
        // works
        Expression<Func<T, bool>> Texpr = x => x.Security.Readers.Any(n => AccessorNames.ToStringArray().Contains(n.Text));

        return Texpr;
    }

请注意,至关重要的是,我需要将其作为表达式进行管理,因为我的数据库驱动程序需要遍历树并转换为本机调用,因此使用编译()进行组合不是一种选择。

以下是我想要与上面的Any()调用相结合的函数。最终输出Expression必须是Expression<Func<T, bool>>类型,我需要将x.Security.Readers传递给这个。

    public static Expression<Func<IEnumerable<EntityName>,bool>> AccessCheckExpression(IEnumerable<EntityName> AccessorNames)
    {
        return accessList => accessList.Any(n => AccessorNames.ToStringArray().Contains(n.Text));
    }

我已经达到了这个目标,但我正在努力解决如何从accessList =>换出accessCheck并让它在accessList中使用 private Expression<Func<T, bool>> ExpressionIsNamed(IEnumerable<EntityName> AccessorNames) { Expression<Func<T, IEnumerable<EntityName>>> accessList = (T x) => x.Security.Readers; Expression<Func<IEnumerable<EntityName>, bool>> accessCheck = SecurityDescriptor.AccessCheckExpression(AccessorNames); // Combine? Expression<Func<T, bool>> Texpr = ??? accessCheck + accessList ??? return Texpr; } 的问题。单一表达。到目前为止,我有这个;

    class ParameterUpdateVisitor : System.Linq.Expressions.ExpressionVisitor
    {
        private ParameterExpression _oldParameter;
        private ParameterExpression _newParameter;

        public ParameterUpdateVisitor(ParameterExpression oldParameter, ParameterExpression newParameter)
        {
            _oldParameter = oldParameter;
            _newParameter = newParameter;
        }

        protected override Expression VisitParameter(ParameterExpression node)
        {
            if (object.ReferenceEquals(node, _oldParameter))
                return _newParameter;

            return base.VisitParameter(node);
        }
    }

    static Expression<Func<T, bool>> UpdateParameter<T>(
        Expression<Func<T, IEnumerable<EntityName>>> expr,
        ParameterExpression newParameter)
    {
        var visitor = new ParameterUpdateVisitor(expr.Parameters[0], newParameter);
        var body = visitor.Visit(expr.Body);

        return Expression.Lambda<Func<T, bool>>(body, newParameter);
    }

[更新]

所以我进一步了;

UpdateParameter(accessList, accessCheck.Parameters[0]);

然后我可以用;

编译
Invoke()

感谢所有InvocationExpression建议,但我的猜测是,当他们访问MongoDB驱动程序时,它不会像Invoke那样。但是,上面的System.ArgumentException: Expression of type 'System.Func`2[MyLib.Project,System.Collections.Generic.IEnumerable`1[MyLib.EntityName]]' cannot be used for parameter of type 'System.Collections.Generic.IEnumerable`1[MyLib.EntityName]' 和我的代码现在都以完全相同的方式失败。即;

x.Security.Readers

因此看起来隐式参数IEnumerable<EntityName>与普通旧{{1}}

的类型不同

2 个答案:

答案 0 :(得分:5)

VisitorExpression是你的朋友。以下是合并类似内容的简化但完整的示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

class Source {
    public List<Value> Values {get;set;}
}
class Value {
    public int FinalValue {get;set;}
}
static class Program {
    static void Main() {
        Expression<Func<Source, IEnumerable<Value>>> f1 =
            source => source.Values;
        Expression<Func<IEnumerable<Value>, bool>> f2 =
            vals => vals.Any(v => v.FinalValue == 3);

        // change the p0 from f2 => f1
        var body = SwapVisitor.Swap(f2.Body, f2.Parameters[0], f1.Body);
        var lambda = Expression.Lambda<Func<Source, bool>>(body,f1.Parameters);
        // which is:
        // source => source.Values.Any(v => (v.FinalValue == 3))
    }

}
class SwapVisitor : ExpressionVisitor {
    private readonly Expression from, to;
    private SwapVisitor(Expression from, Expression to) {
        this.from = from;
        this.to = to;
    }
    public static Expression Swap(Expression body,
        Expression from, Expression to)
    {
        return new SwapVisitor(from, to).Visit(body);
    }
    public override Expression Visit(Expression node)
    {
         return node == from ? to : base.Visit(node);
    }
}

编辑:在您的情况下,这将是:

private Expression<Func<T, bool>> ExpressionIsNamed(
    IEnumerable<EntityName> AccessorNames)
{
    Expression<Func<T, IEnumerable<EntityName>>> accessList =
        (T x) => x.Security.Readers;
    Expression<Func<IEnumerable<EntityName>, bool>> accessCheck =
        SecurityDescriptor.AccessCheckExpression(AccessorNames);


    var body = SwapVisitor.Swap(accessCheck.Body,
        accessCheck.Parameters[0], accessList.Body);
    return Expression.Lambda<Func<T, bool>>(body, accessList.Parameters);
}

答案 1 :(得分:0)

我不确定它是否会起作用,但试试这个:

private Expression<Func<T, bool>> ExpressionIsNamed(IEnumerable<EntityName> AccessorNames)
{
    Expression<Func<T, IEnumerable<EntityName>>> accessList = (x) => x.Security.Readers;
    Expression<Func<IEnumerable<EntityName>, bool>> accessCheck = AccessCheckExpression(AccessorNames);

    var result = Expression.Lambda<Func<T, bool>>(
        Expression.Invoke(accessCheck, accessList.Body), // make invokation of accessCheck, and provide body of accessList (x.Security.Readers) as parameter
        accessList.Parameters.First() // parameter
    );

    return result;
}