I have to dynamically create a Where clause using expressions in the following manner:
Type enumerableType = typeof(Enumerable);
MemberInfo[] members = enumerableType.GetMember("Where*");
MethodInfo whereDef = (MethodInfo)members[0];
Type TSource = whereDef.GetGenericArguments()[0];
Type[] types =
{
typeof(IEnumerable<>).MakeGenericType(TSource),
typeof(Func<,>).MakeGenericType(TSource, typeof(Boolean))
};
MethodInfo method = enumerableType.GetMethod("Where", types);
Expression.Call(null, method , new Expression[]
{
Expression.Lambda(newEX,new ParameterExpression[]{param})
});
The problem I am facing is that at Expresssion.call
I am getting the following error:
Method System.Collections.Generic.IEnumerable`1[TSource] Where[TSource](System.Collections.Generic.IEnumerable`1[TSource], System.Func`2[TSource,System.Boolean]) is a generic method definition
Can some one tell me how to overcome this problem?
P.S: The Expression tree was built by referencing the code generated with reflector. In reflector the code is given as below:
Expression.Call(null, (MethodInfo) methodof(Enumerable.Where),
new Expression[] ...