Expression using the "where" method
I am trying to achieve an expression like this: "A => ABWhere (extExp) .Count ()> 0" and I have a problem with how to make an expression for Where (...) which I assume is an extension method for ICollection <>. Can anyone please help?
Expression<Func<N, bool>> conditions = c => c.T_ID == 1 || c.T_ID == 2;
ParameterExpression mpe = Expression.Parameter(typeof(T), "A");
Expression prop = Expression.Property(mpe,typeof(T).GetProperty("B"));
...
var propWhere = Expression.Call(..., prop, conditions);
How to call it correctly
+3
source to share
1 answer
What happens is an overload of the call that accepts a MethodInfo. To get information about the method, I think it is best to use the code from this answer - fooobar.com/questions/481011 / ...
public static MethodInfo GetMethodInfo(Expression<Action> expression)
{
var member = expression.Body as MethodCallExpression;
if (member != null)
return member.Method;
throw new ArgumentException("Expression is not a method", "expression");
}
Using
var whereMethodInfo = GetMethodInfo(() => Enumerable.Where(Enumerable.Empty<T>(), i=>true));
BTW I suggest you download LINQPad and use it to write queries and view the generated expression trees and IL code.
+1
source to share