This is very similar to this answer however, I don't have type information at build time. I have Type
variable. How would I cast LambdaExpression
to Expression<Func<T,bool>>
where T
is only known during run time?
Update: Here is what i am doing
var funcType1 = typeof(Func<,>).MakeGenericType(nullablePropertyType, typeof(bool));
var funcType2 = typeof(Expression<>).MakeGenericType(funcType1);
var expressionFunc = Expression.Convert((filterPredicate as LambdaExpression), funcType2);
The last line is giving me this error message
"No coercion operator is defined between types 'System.Func`2[MyType,System.Boolean]' and 'System.Linq.Expressions.Expression`1[System.Func`2[MyType,System.Boolean]]'."
Stepping back, i want to do cast because while the lambdaexpression actually have Expression<Func<MyType,bool>>
, I get error saying Linq to Entities can't process it...