基于另一个LINQ表达式和一个值构建特定的LINQ表达式 [英] Build a specific LINQ expression based on another LINQ expression and a value

查看:85
本文介绍了基于另一个LINQ表达式和一个值构建特定的LINQ表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有以下形式的LINQ表达式:

If I've got a LINQ expression of the form:

Expression<Func<MyClass, string, bool>> filterExpression = (x, filterVal) => x.DisplayName.Contains(filterVal);

我有什么办法可以到达下面的表达式?

Is there any way I can get to the expression below?

Expression<Func<MyClass, bool>> filter = x => x.DisplayName.Contains("John");

我需要在Linq-to-Entities Where调用中使用第二个表达式.

I need to use the second expression in a Linq-to-Entities Where call.

推荐答案

您需要编写一个

You need to write an ExpressionVisitor that replaces the ParameterExpression with a ConstantExpression.

看起来像

protected override Expression VisitParameter(ParameterExpression node) {
    if (node.Name == "filterVal")
        return Expression.Constant(something);
    return node;
}

这篇关于基于另一个LINQ表达式和一个值构建特定的LINQ表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆