1

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...

svick
  • 236,525
  • 50
  • 385
  • 514
user21479
  • 1,179
  • 2
  • 13
  • 21
  • You can't "cast" with an unknown type, since all casting does is change how the _compiler_ interacts with the object. Can you share more about what you are trying to accomplish - maybe there's another way to solve it other than casting? – D Stanley Jun 20 '17 at 18:57
  • So if the `filterPredicate` already is of the type you need, but EF cannot understand what's inside it, then no casting whatsoever will help. Rather you have to use a predicate with no expressions in it's body that cannot translate to SQL. – tinudu Aug 12 '17 at 10:32

0 Answers0