Linq - Replace non-generic lambda expression with generic lambda expression

I have this line of code:

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

where TEntityType is a common parm.

However, I do not have universal parma. I have:

Type _EntityType;

What is not the general syntax of an Expression.Lambda expression?

thanks

+3
source share
1 answer

There is an overload for Expression.Lambda, which takes the body type of the expression, so you just need to create the type dynamically before calling this overload.

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

var predicate = Expression.Lambda(lambdaType, body, param);
+8
source

Source: https://habr.com/ru/post/1708229/


All Articles