1

I want to write an expression that will retrieve a property within a property. My 2 classes:

public class BusinessType
{
    public string Proprietor
    {
        get { return "Proprietor"; }
    }
}

public class VendorApplicationViewModel
{
    public List<BusinessType> BusinessClassification { get; set; }
}

public static IHtmlString RadioListForIEnum<TModel, TProperty>(this HtmlHelper<TModel> htmlhelper, 
                                                               Expression<Func<TModel, TProperty>> expression)
{
    var prop = ModelMetadata.FromLambdaExpression(expression, htmlhelper.ViewData);
    //Func<TModel2, TProperty2> nestedProperty = 
    return null;
}

I'm rather lost as to how I can achieve this. Also I'm fairly new to expression trees any good recommendations on tutorials and the likes would be greatly appreciated. Thanks

nawfal
  • 70,104
  • 56
  • 326
  • 368

1 Answers1

1

I think you're simply trying to get a property within a property.

You could have the following.

 Expression<Func<VendorApplicationViewModel, string>> lambda1 = model => model.BusinessClassification[0].Proprietor;

which can be recreated in code as

 ParameterExpression param = Expression.Parameter(typeof(VendorApplicationViewModel));
 Expression<Func<VendorApplicationViewModel, string>> lambda2 = Expression.Lambda<>(
      Expression.Property(
           Expression.Property(
                param,
                "BusinessClassification",
                Expression.Constant(0)
           ),
           "Proprietor"
      ),
      param
 );
Double Down
  • 898
  • 6
  • 13