PredicateBuilder

using System;
using System.Linq;
using System.Linq.Expressions;

namespace Oyang.Tool
{
    public static class PredicateBuilder
    {
        public static Expressionbool>> True() { return f => true; }
        public static Expressionbool>> False() { return f => false; }
        
        public static Expressionbool>> And(this Expressionbool>> expr1, Expressionbool>> expr2)
        {
            var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast());
            return Expression.Lambdabool>>
                  (Expression.AndAlso(expr1.Body, invokedExpr), expr1.Parameters);
        }

        public static Expressionbool>> Or(this Expressionbool>> expr1, Expressionbool>> expr2)
        {
            var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast());
            return Expression.Lambdabool>>
                  (Expression.OrElse(expr1.Body, invokedExpr), expr1.Parameters);
        }

        public static Expressionbool>> And(this Expressionbool>> expr1, Funcbool>>> func)
        {
            Expressionbool>> expr2 = func();
            var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast());
            return Expression.Lambdabool>>
                  (Expression.AndAlso(expr1.Body, invokedExpr), expr1.Parameters);
        }

        public static Expressionbool>> Or(this Expressionbool>> expr1, Funcbool>>> func)
        {
            Expressionbool>> expr2 = func();
            var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast());
            return Expression.Lambdabool>>
                  (Expression.OrElse(expr1.Body, invokedExpr), expr1.Parameters);
        }
    }
}

 

你可能感兴趣的:(PredicateBuilder)