1 #region Lambda公共拼接函数 2 /// <summary> 3 /// LambdaWhere(枚举) 4 /// </summary> 5 public enum LambdaWhere : int 6 { 7 And = 1, 8 Or = 2 9 } 10 /// <summary> 11 /// Lambda公共拼接函数(来自:http://www.cnblogs.com/CielWater) 12 /// </summary> 13 /// <param name="BodyLeft">要拼接的Lambda表达式</param> 14 /// <param name="BodyRight">要拼接的Lambda表达式</param> 15 /// <param name="LambdaWhere">拼接方式,And或者Or</param> 16 /// <returns></returns> 17 public Expression<Func<T, bool>> Lambda<T>(Expression<Func<T, bool>> BodyLeft, Expression<Func<T, bool>> BodyRight, LambdaWhere LambdaWhere) 18 { 19 if (BodyLeft != null && BodyRight != null) 20 { 21 ParameterExpression pe = BodyLeft.Parameters.Single(); 22 switch (LambdaWhere) 23 { 24 case LambdaWhere.And: 25 BodyLeft = Expression.Lambda<Func<T, bool>>( 26 Expression.And(BodyLeft.Body, Expression.Invoke(BodyRight, pe)), pe); 27 break; 28 default: 29 BodyLeft = Expression.Lambda<Func<T, bool>>( 30 Expression.Or(BodyLeft.Body, Expression.Invoke(BodyRight, pe)), pe); 31 break; 32 } 33 } 34 else if (BodyLeft != null) 35 { 36 return BodyLeft; 37 } 38 else 39 { 40 return BodyRight; 41 } 42 return BodyLeft; 43 } 44 #endregion