Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linq - Substitute Non-Generic Lambda Expression for Generic Lambda Expression

Tags:

c#

lambda

linq

I have a this line of code:

var predicate = Expression.Lambda<Func<TEntityType, bool>>(body, param);

where TEntityType is a generic parm.

However, I don't have generic parm available. I do have:

Type _EntityType;

What is the non-generic syntax for Expression.Lambda is this case?

Thanks

like image 964
theBruce Avatar asked May 12 '09 23:05

theBruce


1 Answers

There's an overload for Expression.Lambda that takes the type of the expression body, so you just need to create the type dynamically before calling that overload.

type lambdaType = typeof(Func<,>).MakeGenericType(_EntityType, typeof(bool));

var predicate = Expression.Lambda(lambdaType, body, param);
like image 87
Cameron MacFarland Avatar answered Sep 20 '22 16:09

Cameron MacFarland