I need to construct a dynamic lambda expression like: m => m.Data[0].Name
I have model classes:
public class GridItem
{
[Required]
[Display(Name = "Name")]
public string Name{ get; set; }
[Required]
[Display(Name = "Address")]
public string Password { get; set; }
[Display(Name = "Age")]
public string Age{ get; set; }
}
public class GridModelList
{
public List<GridItem> Data { get; set; }
public GridModelList()
{
Data = new List<GridItem>();
Data.Add(new GridItem() {Name = "Name", Address= "Address", Age= 1 });
Data.Add(new GridItem() {Name = "Name", Address= "Address", Age= 1 });
}
}
And I am trying to consruct it like so:
ParameterExpression fieldName = Expression.Parameter(typeof(TGridModel), "m");
MemberExpression fieldExpr = Expression.Property(fieldName, "m.Data.getItem(0).Name");
Expression<Func<TGridModel, object>> exp = Expression.Lambda<Func<TGridModel, object>>(fieldExpr, fieldName);
However, my problem is that I don't know how to use '.getItem(0)' properly, as it gives me exception:
Instance property 'Data.get_Item(0).Name' is not defined for type 'GridModelList'
Any help on how to build the needed expression m => m.Data[0].Name would be greatly appreciated!!
Func<TGridModel, object> lambda = m => m.Data[0].Name;?