将包含转换为表达式树 [英] Convert Contains To Expression Tree
问题描述
与以下内容相关:创建具有3个条件的Lambda表达式
请考虑以下代码:
from a in myTbl
where a.Address.Contains(strToCheck)
select a
如何将其转换为表达式树并使用表达式编写以上代码?
主要问题是将a.Address.Contains(strToCheck)
转换为Expression Tree
.
How I can convert this to Expression Tree and write above code with Expressions?
Main Problem is converting a.Address.Contains(strToCheck)
to Expression Tree
.
编辑1)地址是 string
字段,而strToCheck
是 string
Edit 1) Address is a string
field and strToCheck
is a string
谢谢
推荐答案
a.Address.Contains(strToCheck)
表示对
a.Address.Contains(strToCheck)
represents a call to string.Contains
instance method on a.Address
instance with strToCheck
argument.
构建相应表达式的最简单方法是使用以下 Expression.Call
重载:
The simplest way to build the corresponding expression is to use the following Expression.Call
overload:
public static MethodCallExpression Call(
Expression instance,
string methodName,
Type[] typeArguments,
params Expression[] arguments
)
像这样(使用链接的问题中的术语):
like this (using the terms from the linked question):
var body = Expression.Call(
Expression.PropertyOrField(param, "Address"), // instance
"Contains", // method
Type.EmptyTypes, // no generic type arguments
Expression.Constant(strToCheck) // argument
);
这篇关于将包含转换为表达式树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!