How to convert between Linq expressions with different return types?
You'll need to create a new expression by:
- Using
Expression.Convert
over the source expression's body to create the result's body. - Using this body and reusing the parameters of the source expression to create the transformed lambda expression with
Expression.Lambda
.
Try this:
Expression<Func<T, object>> source = ...
var resultBody = Expression.Convert(source.Body, typeof(U));
var result = Expression.Lambda<Func<T, U>>(resultBody, source.Parameters);