Extending a lambda expression

I have an existing lambda expression that was created as:

Expression<Func<Entities.Area, bool>> where = (x => (x.Created > this.Value || (x.Changed != null && x.Changed > this.Value)));

      

Now I have to expand this expression like this:

Expression<Func<Entities.Area, bool>> whereAdd = (x => x.Client.Id == ClientInfo.CurrentClient.Id);

      

The result should look like this:

Expression<Func<Entities.Area, bool>> where = (x => (x.Created > this.Value || (x.Changed != null && x.Changed > this.Value)) && x.Client.Id == ClientInfo.CurrentClient.Id);

      

I cannot change the creation of the first expression directly because this is not my code.

I hope someone can help me how to extend the first lambda expression.

+3


source to share


2 answers


Just create a new expression AndAlso

, taking the bodies of both of your expressions and making a new lambda expression out of it:



ParameterExpression param = Expression.Parameter(typeof(Entities.Area), "x");
Expression body = Expression.AndAlso(where.Body, whereAdd.Body);

var newWhere = Expression.Lambda<Func<Entities.Area, bool>>(body, param);

Console.WriteLine(newWhere.ToString());
// x => (((x.Created > Convert(value(UserQuery).Value)) OrElse ((x.Changed != null) AndAlso (x.Changed > Convert(value(UserQuery).Value)))) AndAlso (x.Client.Id == ClientInfo.CurrentClient.Id))

      

+2


source


Combining two expressions (Expression <Func <T, bool →)



var body = Expression.AndAlso(where.Body, whereadd.Body);
var lambda = Expression.Lambda<Func<Entities.Area,bool>>(body, where.Parameters[0]);

      

-1


source







All Articles