5

使用来自各种 SO 帖子的信息,尤其是这个博客(更正为使用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;
    }

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

Any()所以下面是我想与上面的调用结合的功能。最终的输出表达式需要是类型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 =>accessCheckaccessList单个表达式中使用它。到目前为止,我有这个;

    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()建议,但我的猜测是,当他们使用 MongoDB 驱动程序时,它不会喜欢InvocationExpression. 但是,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>

4

2 回答 2

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);
}
于 2012-11-22T13:11:56.097 回答
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;
}
于 2012-11-22T13:13:12.200 回答