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

1
  • 1
    Its extremely difficult to decipher what you're asking here. Commented Jun 2, 2013 at 15:52

1 Answer 1

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
 );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.