基于另一个LINQ表达式和一个值构建特定的LINQ表达式

编程入门 行业动态 更新时间:2024-10-19 06:17:23
本文介绍了基于另一个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表达式

本文发布于:2023-11-17 02:08:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608419.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表达式   LINQ

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!