如何从表达式谓词中获取Not表达式谓词?

时间:2011-01-16 15:36:51

标签: .net expression-trees

有特定于域的类:

public class Account : IEntity
{
    public bool IsActive { get; set; }
    public DateTime ExpirationDate { get; set; }
}

以及从数据源获取帐户的一些谓词:

Expression<Func<Account, bool>> predicate = a => a.IsActive == false && a.ExpirationDate < DateTime.Now;

List<Account> lst = new List<Account>();
lst.AsQueryable().Where(predicate);

如何从谓词中生成not_predicate? not_predicate在哪里:

!(a.IsActive == false && a.ExpirationDate < DateTime.Now)

1 个答案:

答案 0 :(得分:2)

这可能需要调整(我不是在PC上) - 但是类似于:

var not = Expression.Lambda<Func<Account,bool>>(
    Expression.Not(predicate.Body), predicate.Parameters);
相关问题