zoukankan      html  css  js  c++  java
  • PredicateBuilder

    using System;
    using System.Linq;
    using System.Linq.Expressions;
    
    namespace Oyang.Tool
    {
        public static class PredicateBuilder
        {
            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>> And<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2)
            {
                var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
                return Expression.Lambda<Func<T, bool>>
                      (Expression.AndAlso(expr1.Body, invokedExpr), expr1.Parameters);
            }
    
            public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2)
            {
                var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
                return Expression.Lambda<Func<T, bool>>
                      (Expression.OrElse(expr1.Body, invokedExpr), expr1.Parameters);
            }
    
            public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1, Func<Expression<Func<T, bool>>> func)
            {
                Expression<Func<T, bool>> expr2 = func();
                var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
                return Expression.Lambda<Func<T, bool>>
                      (Expression.AndAlso(expr1.Body, invokedExpr), expr1.Parameters);
            }
    
            public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1, Func<Expression<Func<T, bool>>> func)
            {
                Expression<Func<T, bool>> expr2 = func();
                var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
                return Expression.Lambda<Func<T, bool>>
                      (Expression.OrElse(expr1.Body, invokedExpr), expr1.Parameters);
            }
        }
    }
  • 相关阅读:
    套接字描述符在多进程和多线程下的共享
    广播和多播
    原始套接字和数据链路层访问
    Libevent:11使用Libevent的DNS上层和底层功能
    Lib1vent:10链接监听器接受TCP链接
    Libevent:9Evbuffers缓存IO的实用功能
    Libevent:7Bufferevents基本概念
    python生成url测试用例
    OMD开源监控软件
    iptable防范ddos攻击
  • 原文地址:https://www.cnblogs.com/oyang168/p/9952008.html
Copyright © 2011-2022 走看看