【发布时间】:2012-01-10 09:20:12
【问题描述】:
假设我们有两个类
public class EntityA
{
public EntityB EntityB { get; set; }
}
public class EntityB
{
public string Name { get; set; }
public bool IsDeleted { get; set; }
}
还有选择器和谓词的两个表达式
Expression<Func<EntityA, EntityB>> selector = c => c.EntityB;
Expression<Func<EntityB, bool>> predicate = c => c.IsDeleted && c.Name == "AAA";
我需要编写一个返回组合表达式的方法,例如
Expression<Func<TSource, bool>> Compose<TPropType>(Expression<Func<TSource, TPropType>> selector, Expression<Func<TPropType, bool>> predicator)
{
// Expression API ???
}
在我的示例中,结果应该是
Expression<Func<EntityA, bool>> exp = Compose(selector, predicate);
什么等价于
Expression<Func<EntityA, bool>> exp = c => c.EntityB.IsDeleted && c.EntityB.Name == "AAA";
提前致谢。
【问题讨论】:
-
最后一个
c.Name == "AAA"应该是c.EntityB.Name == "AAA",对吧? -
是的,你是对的。已更正。
标签: linq entity-framework lambda expression-trees