0

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[] ...
Cœur
  • 37,241
  • 25
  • 195
  • 267
user119020
  • 413
  • 1
  • 3
  • 13
  • What are you trying to accomplish? – Grax32 Dec 22 '14 at 21:00
  • im trying to dynamically create the Where clause.Any how i have found the mistake. I had to use Method.MakeGenericMethod(); before passing it into Expression.Call() – user119020 Dec 23 '14 at 00:29
  • If you solved your problem, please post the solution as an answer and accept it. Other users might find it helpful. – Marco May 11 '16 at 12:57
  • The answer in below link may helpful http://stackoverflow.com/questions/25884233/how-to-use-expressions-to-invoke-a-method-call-with-a-generic-list-as-the-parame – Vi Tran Jan 06 '17 at 09:48

0 Answers0