本文介绍了基于另一个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.
推荐答案您需要编写一个 ExpressionVisitor ,将ParameterExpression替换为ConstantExpression.
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表达式
发布评论