123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Threading.Tasks;
- namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
- {
- /// <summary>
- //
- /*
- Expression<Func<UserInfo, bool>> exp = x => x.Status == (int)BizConst.UserStatus.Active;
- if(parameters.IsAuthenticated)
- {
- exp = exp.And(x => x.IdentityAuth == (int) BizConst.IdentityAuthStatus.Authed && x.CareerAuth == (int) BizConst.GlobalCareerStatus.Authed);
- }
- if(parameters.IsVip)
- {
- exp = exp.And(x => x.Vip != (int) BizConst.VipTag.NoVip);
- }*/
- /// </summary>
- public static class PredicateExtensions
- {
- ///// <summary>
- ///// 机关函数应用True时:单个AND有效,多个AND有效;单个OR无效,多个OR无效;混应时写在AND后的OR有效。即,设置为True时所有or语句应该放在and语句之后,否则无效
- ///// </summary>
- //public static Expression<Func<T, bool>> True<T>() { return f => true; }
- ///// <summary>
- ///// 机关函数应用False时:单个AND无效,多个AND无效;单个OR有效,多个OR有效;混应时写在OR后面的AND有效。 即,设置为False时所有or语句应该放在and语句之前,否则无效
- ///// </summary>
- //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);
- //}
- /// <summary>
- /// 机关函数应用True时:单个AND有效,多个AND有效;单个OR无效,多个OR无效;混应时写在AND后的OR有效。即,设置为True时所有or语句应该放在and语句之后,否则无效
- /// </summary>
- public static Expression<Func<T, bool>> BaseAnd<T>() { return f => true; }
- /// <summary>
- /// 机关函数应用False时:单个AND无效,多个AND无效;单个OR有效,多个OR有效;混应时写在OR后面的AND有效。 即,设置为False时所有or语句应该放在and语句之前,否则无效
- /// </summary>
- public static Expression<Func<T, bool>> BaseOr<T>() { return f => false; }
- public static Expression<Func<T, bool>> Or<T>(
- this Expression<Func<T, bool>> expr1,
- Expression<Func<T, bool>> expr2)
- {
- var secondBody = expr2.Body.Replace(expr2.Parameters[0], expr1.Parameters[0]);
- return Expression.Lambda<Func<T, bool>>
- (Expression.OrElse(expr1.Body, secondBody), expr1.Parameters);
- }
- public static Expression<Func<T, bool>> And<T>(
- this Expression<Func<T, bool>> expr1,
- Expression<Func<T, bool>> expr2)
- {
- var secondBody = expr2.Body.Replace(expr2.Parameters[0], expr1.Parameters[0]);
- return Expression.Lambda<Func<T, bool>>
- (Expression.AndAlso(expr1.Body, secondBody), expr1.Parameters);
- }
- public static Expression Replace(this Expression expression,
- Expression searchEx, Expression replaceEx)
- {
- return new ReplaceVisitor(searchEx, replaceEx).Visit(expression);
- }
- public static Expression<Func<T, bool>> CombineOrPreicatesWithAndPredicates<T>(this Expression<Func<T, bool>> combinedPredicate,
- Expression<Func<T, bool>> andPredicate, Expression<Func<T, bool>> orPredicate)
- {
- combinedPredicate = combinedPredicate ?? BaseAnd<T>();
- if (andPredicate != null && orPredicate != null)
- {
- andPredicate = andPredicate.And(orPredicate);
- combinedPredicate = combinedPredicate.And(andPredicate);
- }
- else if (orPredicate != null)
- {
- combinedPredicate = combinedPredicate.And(orPredicate);
- }
- else
- {
- combinedPredicate = combinedPredicate.And(andPredicate);
- }
- return combinedPredicate;
- }
- public static void AddToPredicateTypeBasedOnIfAndOrOr<T>(ref Expression<Func<T, bool>> andPredicate,
- ref Expression<Func<T, bool>> orPredicate, Expression<Func<T, bool>> newExpression, bool isAnd)
- {
- if (isAnd)
- {
- andPredicate = andPredicate ?? BaseAnd<T>();
- andPredicate = andPredicate.And(newExpression);
- }
- else
- {
- orPredicate = orPredicate ?? BaseOr<T>();
- orPredicate = orPredicate.Or(newExpression);
- }
- }
- }
- internal class ReplaceVisitor : ExpressionVisitor
- {
- private readonly Expression from, to;
- public ReplaceVisitor(Expression from, Expression to)
- {
- this.from = from;
- this.to = to;
- }
- public override Expression Visit(Expression node)
- {
- return node == from ? to : base.Visit(node);
- }
- }
- }
|