Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expression.Lambda and query generation at runtime, nested property “Where” example

I found very nice answer on a question about building Expression Tree for Where query.

Expression.Lambda and query generation at runtime, simplest "Where" example

Can someone help me and show me how this example could be implemented in the scenario with nested property. I mean instead of:

var result = query.Where(item => item.Name == "Soap")

With that solution:

var item = Expression.Parameter(typeof(Item), "item");

var prop = Expression.Property(item, "Name");

var soap = Expression.Constant("Soap");

var equal = Expression.Equal(prop, soap);

var lambda = Expression.Lambda<Func<Item, bool>>(equal, item);

var result = queryableData.Where(lambda);

How can I build the tree for the following?

var result = query.Where(item => item.Data.Name == "Soap").
like image 490
Piotr Czarnecki Avatar asked Dec 14 '15 15:12

Piotr Czarnecki


People also ask

What is lambda expression in C# with example?

The expression num => num * 5 is a lambda expression. The => operator is called the "lambda operator". In this example, num is an input parameter to the anonymous function, and the return value of this function is num * 5 . So when multiplyByFive is called with a parameter of 7 , the result is 7 * 5 , or 35 .

Can we use dynamic in lambda expression in C#?

This is identical to the lambda expression in the simple lambda expression query listed above. The only difference is that instead of embedding the definition in the code, you've built it dynamically. The last step before using the dynamic lambda expression is to compile it: var compiledLambda = lambda.

What is Expression C#?

An expression in C# is a combination of operands (variables, literals, method calls) and operators that can be evaluated to a single value. To be precise, an expression must have at least one operand but may not have any operator.


2 Answers

This is the same answer as posted above, but I find this more readable in terms of visualizing an expression tree:

var parameterItem = Expression.Parameter(typeof(Item), "item");

var lambda = Expression.Lambda<Func<Item, bool>>(
    Expression.Equal(
        Expression.Property(
            Expression.Property(
                parameterItem, 
                "Data"
            ), 
            "Name"
        ), 
        Expression.Constant("Soap")
    ), 
    parameterItem
);

var result = queryableData.Where(lambda);
like image 113
Shlomo Avatar answered Sep 21 '22 04:09

Shlomo


(This answer was originally posted by the OP in the question.)

The problem can be solved with:

var item = Expression.Parameter(typeof(Item), "item");

var dataExpr = Expression.Property(item, "Data");

var prop = Expression.Property(dataExpr, "Name");

var soap = Expression.Constant("Soap");

var equal = Expression.Equal(prop, soap);

var lambda = Expression.Lambda<Func<Item, bool>>(equal, item);

var result = queryableData.Where(lambda);
like image 43
3 revs, 2 users 90% Avatar answered Sep 21 '22 04:09

3 revs, 2 users 90%