Suppose I have:
class Foo {
public String Bar { get; set; }
}
and I have an Expression<Func<TIn,TOut>> where TOut is constrained to Foo. How can I create an Expression<Func<TIn, String>> for accessing the Bar property?
Edit:
The reason I want this is to write an HtmlHelper for ASP.NET MVC for a complex type – e.g. MyExtension(this HtmlHelper that, Expression<Func<TIn, TOut>> expression) that then calls other HtmlHelper extension methods for properties of the complex type – e.g EditorFor, etc)
You can use
Bodyto access the body of the first expression, thenExpression.Property()to access the property and finallyExpression.Lambda()to combine it all into a lambda:If you can use LINQKit this is somewhat simpler (don’ forget
using LinqKit;):