My ultimate goal is to iterate over nested properties in a lambda expression and determine if any of the properties is null, but I had a problem creating a new lambda expression based on a member expression.
Take this dummy method:
public static void DoStuff<TModelDetail, TValue>(Expression<Func<TModelDetail, TValue>> expr, TModelDetail detail)
{
var memberExpression = expr.Body as MemberExpression;
if (memberExpression == null && expr.Body is UnaryExpression)
{
memberExpression = ((UnaryExpression)expr.Body).Operand as MemberExpression;
}
var pe = Expression.Parameter(typeof(TModelDetail), "x");
var convert = Expression.Convert(memberExpression, typeof(object));
var wee = Expression.Lambda<Func<TModelDetail, object>>(convert, pe);
var hey = wee.Compile()(detail);
}
The following error appears in the Compile.exec line:
variable 'x' of type 'Blah' referring to scope '' but not defined
where Blah is a type of TModelDetail.
How to create lambda using MemberExpression expression? What I ultimately want to do is recursively find the expression of the root member, determine if it is zero, and bubble up and determine if each subsequent expression of the member is equal to zero.