0

I have a situation where I need to logically-combine two expressions:

Expression<Func<T1, bool>> expr1; Expression<Func<T2, bool>> expr2; In some cases, T1 and T2 are the same type, and in those cases, I want to logically combine the bodies of the two expressions (using && or ||), rather than combine calls to each of them. but I can't combine them because expr1 can't be cast to the type of expr2.

So, in the case where typeof(T1) == typeof(T2), what is the best way to logically-combine the bodies of the two expressions such that they both operate on the argument type T1?

Unable to cast variable of type Expression<Func<T1, bool>> to Expression<Func<T2, bool>>

3
  • 1
    Can you please add some minimal reproducible example?
    – Guru Stron
    Commented Jun 28 at 20:17
  • I have made such function earlier. Check ExpressionTypeMapper from my answer. After mapping you can cast expression to replacement type. Commented Jun 28 at 22:13
  • The reproducible example is Expression<T1, bool> expr = (Expression<T1, bool>) value;, where value is declared as Expression<T2, bool>' in the case where typeof(T1) == typeof(T2)` is true.
    – caddzooks
    Commented Jun 29 at 0:23

0

Browse other questions tagged or ask your own question.