/// <summary>/// 構造函數使用True時:單個AND有效,多個AND有效;單個OR無效,多個OR無效;混合時寫在AND后的OR有效/// 構造函數使用False時:單個AND無效,多個AND無效;單個OR有效,多個OR有效;混合時寫在OR后面的AND有效/// </summary>public static class PRedicateExtensions{ public static Expression<Func<T, bool>> True<T>() { return f => true; } public static Expression<Func<T, bool>> False<T>() { return f => false; } public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expression1, Expression<Func<T, bool>> expression2) { var invokedExpression = Expression.Invoke(expression2, expression1.Parameters.Cast<Expression>()); return Expression.Lambda<Func<T, bool>>(Expression.Or(expression1.Body, invokedExpression), expression1.Parameters); } public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expression1, Expression<Func<T, bool>> expression2) { var invokedExpression = Expression.Invoke(expression2, expression1.Parameters.Cast<Expression>()); return Expression.Lambda<Func<T, bool>>(Expression.And(expression1.Body, invokedExpression), expression1.Parameters); }}
調用方法:
HUBDataDataContext db = new HUBDataDataContext();var predicate = PredicateExtensions.False <T_DISPATCH>();predicate = predicate.Or(c => c.ToHUBID == 1);var dispatch = db.T_DISPATCH.Where(predicate).Where(c => c.State == 3);
轉自:http://www.cnblogs.com/ejiyuan/archive/2009/07/14/1523515.html
新聞熱點
疑難解答