c# - 扩展 lambda 表达式

标签 c# lambda

我有一个现有的 lambda 表达式,其创建方式如下:

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

现在,我必须用这个来扩展这个表达式:

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

结果应该是这样的:

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

我无法直接更改第一个表达式的创建,因为它不是我的代码。

我希望有人能帮助我如何扩展第一个 lambda 表达式。

最佳答案

只需创建一个新的 AndAlso 表达式,获取两个表达式的主体并从中创建一个新的 lambda 表达式:

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))

关于c# - 扩展 lambda 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30694565/

相关文章:

c# - Swagger for Asp.Net Mvc 网站

c# - 如何在 MVC 中的另一个页面中显示 html 页面的一部分

java - lambda 性能的差异?

c# - ASP.NET : Possible to create a UserControl that include inside markup content?

c# - 在不同存储库中共享类似的 linq to 实体表达逻辑的建议做法是什么

c# - 序列化名为 "return"的 JSON 对象

c++ - 编译器方差 : type of `this` when value-captured in mutable lambda defined in a const member function

c# - 解释 c# lambda 语法

Java8 : convert one map to another map using stream

visual-studio-2010 - 频繁的VS 2010速度非常慢,并且在特定的解决方案/文件上崩溃?