1

I have 3 classes.

    AutoYearMake{
       int Year { get; set; }
       string Make { get; set; }
            }

    AutoModel{
       IAutoYearMake AutoYearMakeParent { get; set; }
       string Model { get; set; }
            } 

    AutoTrim{
        IAutoModel AutoModelParent { get; set; }
        string Trim { get; set; }
            }

I need to create a query to a database. How can I get dynamically an expression like:

   Expression<Func<AutoTrim, bool>> expression =  expression = t => t.AutoModelParent.AutoYearMakeParent.Year == year.Value
                              && t.AutoModelParent.AutoYearMakeParent.Make 
                              == make && t.AutoModelParent.Model == model; 

This is my code. It doesn't work.

        ParameterExpression parameter = Expression.Parameter(typeof (AutoTrim), "a");

        MemberExpression yearProp = Expression.Property(parameter, "AutoModelParent.AutoYearMakeParent.Year");
        MemberExpression makeProp= Expression.Property(parameter, "AutoModelParent.AutoYearMakeParent.Make");
        MemberExpression modelProp= Expression.Property(parameter, "AutoModelParent.Model");

        Expression right = Expression.Constant(2014);
        Expression e1 = Expression.Equal(yearProp, right);
        right = Expression.Constant("make");
        Expression e2 = Expression.Equal(makeProp, right);
        right = Expression.Constant("model");
        Expression e3 = Expression.Equal(modelProp, right);

        Expression predicateBody = Expression.AndAlso(e1, e2);
        Expression final = Expression.AndAlso(e1, e2); 

How can I resolve this problem? I tried to use Expression.Call. It wasn't right way.

1
  • Look at the expression expression and see how it is structured.
    – usr
    Jul 1, 2014 at 12:04

3 Answers 3

4

The first argument of the Expression.Property() methods is object from which property should be accessed. If you want to access property AutoModelParent from parameter t you use:

Expression.Property(parameter, "AutoModelParent")

So, if you want to access Model property of AutoModelParent property of parameter t you can go with:

Expression.Property(Expression.Property(parameter, "AutoModelParent"), "Model")
0

Consider this solution:

private static MemberExpression GetPropertyPathAccessor(Expression parameter, string path)
{
    return (MemberExpression) path.Split('.').Aggregate(parameter, Expression.Property);
}

or if you don't like one-liners

private static MemberExpression GetPropertyPathAccessor(Expression parameter, string path)
{
    Expression current = parameter;
    foreach (var propertyName in path.Split('.'))
    {
        current = Expression.Property(current, propertyName);
    }
    return (MemberExpression)current;
}

Then you can use:

MemberExpression yearProp = GetPropertyPathAccessor(parameter, "AutoModelParent.AutoYearMakeParent.Year");
0

How about this?

After that you have the Func<> in expFunc and you can use it right away.

var argParam = Expression.Parameter(typeof(AutoTrim), "s");
var expFunc = Expression.Lambda<Func<AutoTrim, bool>>(
        Expression.AndAlso(
            Expression.AndAlso(
                Expression.Equal(
                    Expression.Property(Expression.Property(Expression.Property(argParam, "AutoModelParent"), "AutoYearMakeParent"), "Year"), 
                    Expression.Constant(year.Value)),
                Expression.Equal(
                    Expression.Property(Expression.Property(Expression.Property(argParam, "AutoModelParent"), "AutoYearMakeParent"), "Make"),
                    Expression.Constant(make))
            ),
            Expression.Equal(
                Expression.Property(Expression.Property(argParam, "AutoModelParent"), "Model"), 
                Expression.Constant(model))
        ),
        argParam
    ).Compile();

But of course you need these too (just example values):

int? year = 2000;
string make = "BMW";
string model = "6";

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.