创建一个多级MemberExpression

时间:2013-03-08 15:14:31

标签: c# linq

我知道如何创建MemberExpression例如entity.Property

// Expression: "entity"
var parameter = Expression.Parameter(entityType, "entity");

// Expression: "entity.Property"
var property = Expression.MakeMemberAccess(parameter, entityType.GetProperty(propertyName));

但我想创建一个MemberExpression来访问子属性,例如entity.Property.SubProperty

我该怎么做?

1 个答案:

答案 0 :(得分:2)

  var parameter = Expression.Parameter(entityType, "entity");

   // Expression: "entity.Property"
   var property = Expression.Property(parameter, propertyName);
   var subProperty = Expression.Property(property, subPropertyName);