If I build an expression tree by hand, the run-time seems to automatically determine the return type. So if I build an expression tree which looks like this:
// Order contains a navigation property for Customer (Order o) => o.Customer;
the run-time is determining the return type as Customer, essentially this:
Expression<Func<Order, Customer>> efd = (Order o) => o.Customer;
How can I build it - or change what I built, so the return is object, i.e. the expression would be:
Expression<Func<Order, object>> ef = (Order o) => o.Customer;
This is generic and I don't know at compile time that the return type would be Customer; it could be any navigation propery from (in this case) order.
Let's say I start with an expression like this:
Expression<Func<OrderDTO, object>> ef = (OrderDTO o) => o.Customer;
I have a routine which is rebuilding this, changing the type of
Order by walking the tree and changing the types based on a map of from / to types. That's all I'm doing, but the resulting expression is
Expression<Func<Order, Customer>> ef = (Order o) => o.Customer;
So when I rebuild the tree, I need to specify the return type somehow - it looks as if the system is automatically determining the return type as I didn't specify it anyway. Thanks, Ray
It's hard to say without actually seeing your code, but it looks like you're using on of the versions of
Expression.Lambda() that don't let you specify the type of the created expression. If you do that, you're right that the delegate type will be decided automatically.
To fix this, you either need to use a version of
Expression.Lambda() that lets you specify the delegate type using a type parameter (e.g.
Expression.Lambda<Func<Order, object>>(â€¦)), or, more likely in your case, the version where the delegate type is specified as a normal parameter of type