How to create a Lambda Expression querying values from a composition with C #

0

Today I can create a query expression using the following code:

var endereco = new Endereco { Id = 4 };

var param = Expression.Parameter(typeof(Usuario), "");

var ex = Expression.Lambda<Func<Usuario, bool>>(
          Expression.Equal(
            Expression.Property(param, "Endereco"),
            Expression.Constant(endereco)
          ), param);

I have a user who has a Address that has a City. I need to see users by city. It would look something like the code below:

var cidade = new Cidade { Id = 4 };

var param = Expression.Parameter(typeof(Usuario), "");

var ex = Expression.Lambda<Func<Usuario, bool>>(
           Expression.Equal(
             Expression.Property(param, "Endereco.Cidade"),
             Expression.Constant(cidade)
           ), param);

But it has to be dynamic expression, because the terms Address and City are dynamic and need to be text necessarily.

I've been searching for hours without success. If anyone can help me, thank you very much.

    
asked by anonymous 10.03.2018 / 18:26

0 answers