2011-01-16 71 views
2

有特定於域的類:如何從表達式Predicate中獲取Not expression謂詞?

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) 

回答

2

這可能需要調整輕視(我不是在PC) - 但這樣的:

var not = Expression.Lambda<Func<Account,bool>>(
    Expression.Not(predicate.Body), predicate.Parameters); 
相關問題