Related To: Create a Lambda Expression With 3 conditions
Please consider this Code:
from a in myTbl
where a.Address.Contains(strToCheck)
select a
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
.
Edit 1) Address is a string
field and strToCheck
is a string
Thanks
a.Address.Contains(strToCheck)
represents a call to string.Contains
instance method on a.Address
instance with strToCheck
argument.
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
);