c# - 如何使用 Expression(Of TDelegate).Update 方法

标签 c# lambda expression

我已经使用 Lambda 表达式构建了一个存储库来过滤我的实体集合。作为我发送的方法的参数 Expression<Func<Case, bool>> exp .但在方法内部,我想用一些全局过滤器更新同一个表达式。我可以看到表达式对象本身有一个 Update 方法,但我不知道它是如何实现的(并且在搜索网络时找不到任何东西)。

exp.Update(exp.Body, ???);

谁能举个例子??

编辑:方法的定义:http://msdn.microsoft.com/en-us/library/ee378255.aspx

EDIT2:这是我的代码(我尝试使用 .And 的地方):

Expression<Func<Case, bool>> newExp = c => c.CaseStatusId != (int)CaseStatus.Finished
var binExp = Expression.And(exp.Body, newExp.Body);
ParameterExpression paramExp = Expression.Parameter(typeof(Expression<Func<Case, bool>>), "c");
return repository.Where(Expression.Lambda<Expression<Func<Case, bool>>>(binExp, 
    new[] { paramExp }).Compile()).ToArray();

失败并出现以下 ArgumentException:Lambda 类型参数必须派生自 System.Delegate

最佳答案

我认为 Update 方法在这里不能帮助您。它只会创建一个新的 lambda,但不会用新的更新原始参数,您必须手动进行。 我建议让访问者替换参数,然后您可以将这些表达式放在一起。

总的来说你会得到这样的东西:

    private Case[] getItems(Expression<Func<Case, bool>> exp)
    {
        return repository.Where(AddGlobalFilters(exp).Compile()).ToArray();
    }

    private Expression<Func<Case, bool>> AddGlobalFilters(Expression<Func<Case, bool>> exp)
    {
        // get the global filter
        Expression<Func<Case, bool>> newExp = c => c.CaseStatusId != (int)CaseStatus.Finished;

        // get the visitor
        var visitor = new ParameterUpdateVisitor(newExp.Parameters.First(), exp.Parameters.First());
        // replace the parameter in the expression just created
        newExp = visitor.Visit(newExp) as Expression<Func<Case, bool>>;

        // now you can and together the two expressions
        var binExp = Expression.And(exp.Body, newExp.Body);
        // and return a new lambda, that will do what you want. NOTE that the binExp has reference only to te newExp.Parameters[0] (there is only 1) parameter, and no other
        return Expression.Lambda<Func<Case, bool>>(binExp, newExp.Parameters);
    }


    /// <summary>
    /// updates the parameter in the expression
    /// </summary>
    class ParameterUpdateVisitor : 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);
        }
    }

关于c# - 如何使用 Expression(Of TDelegate).Update 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8257327/

相关文章:

c# - C# 和 PowerShell 中的非统一 System.DateTime.Now 默认格式

c# - 带日期时间的 Firebird 数据库查询

python - 在 Markdown 中取消/注释掉 html 标签的巧妙方法

c++ - 将 Objective-C block 传递给 C++ lambda

C 表达式及其执行

java - 平衡字符串正则表达式

c# - 为什么在使用 SSL 时 SmtpClient 会失败并出现几个不同的异常?

c# - Angular - WCF RESTful 服务 - 发送日期时间

c# - Entity Framework : Unable to create a constant value of type 'System.Collections.Generic.IList` 1'

c# - Expression<Func<TModel,TProperty>> 作为对象初始化的属性?