表达< func< tentity,int,bool>设置int值

发布于 2025-02-03 03:05:54 字数 334 浏览 1 评论 0原文

 public Expression<Func<TEntity, int, bool>> FilterExpr

表达

int i = 12;
//Pseudo Code which sets the value of the second Parameter
FilterExpr.Arg2 = i;

这个

Expression<Func<TEntity, bool>>

I have this Expression

 public Expression<Func<TEntity, int, bool>> FilterExpr

And Now I want to eliminate/insert the value for the int value

like

int i = 12;
//Pseudo Code which sets the value of the second Parameter
FilterExpr.Arg2 = i;

the result should be a

Expression<Func<TEntity, bool>>

without the int parameter

thx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

卸妝后依然美 2025-02-10 03:05:54

您可以通过更换人体中的参数并使用一个参数创建新的lambda表达式来创建新的表达式。例如,使用此替换器(或 替换expressionVisitor 如果您使用的是EF Core 3.0+):

public static class ExpressionExt
{
    public static Expression ReplaceParameter(this Expression expression, ParameterExpression source, Expression target)
    {
        return new ParameterReplacingVisitor { Source = source, Target = target }.Visit(expression);
    }

    class ParameterReplacingVisitor : ExpressionVisitor
    {
        public ParameterExpression Source;
        public Expression Target;
        protected override Expression VisitParameter(ParameterExpression node)
        {
            return node == Source ? Target : base.VisitParameter(node);
        }
    }
}

并修改表达式:

var withReplaced = FilterExpr.Body.ReplaceParameter(
    FilterExpr.Parameters[1], // replace second parameter 
    Expression.Constant(42)); // with some constant
// build new expression with only one parameter by reusing first parameter of original one
var result = Expression.Lambda<Func<MyClass, bool>>(withReplaced, FilterExpr.Parameters[0]);

You can create a new expression by replacing parameter in the body and creating new lambda expression with one parameter. For example using this replacer (or take ReplacingExpressionVisitor if you are using EF Core 3.0+):

public static class ExpressionExt
{
    public static Expression ReplaceParameter(this Expression expression, ParameterExpression source, Expression target)
    {
        return new ParameterReplacingVisitor { Source = source, Target = target }.Visit(expression);
    }

    class ParameterReplacingVisitor : ExpressionVisitor
    {
        public ParameterExpression Source;
        public Expression Target;
        protected override Expression VisitParameter(ParameterExpression node)
        {
            return node == Source ? Target : base.VisitParameter(node);
        }
    }
}

And modify expression:

var withReplaced = FilterExpr.Body.ReplaceParameter(
    FilterExpr.Parameters[1], // replace second parameter 
    Expression.Constant(42)); // with some constant
// build new expression with only one parameter by reusing first parameter of original one
var result = Expression.Lambda<Func<MyClass, bool>>(withReplaced, FilterExpr.Parameters[0]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文