• Expression多条件筛选


    使用场景:多条件筛选,不确定条件的个数时,可选择此种方式;

    扩展类:

     public static class ExpLinqExpressions
        {
            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>> expr1, Expression<Func<T, bool>> expr2)
            { 
                var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>()); 
                return Expression.Lambda<Func<T, bool>>(Expression.Or(expr1.Body, invokedExpr), expr1.Parameters); 
            }
            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.And(expr1.Body, invokedExpr), expr1.Parameters); 
            }
        }
    

      

    使用样例:

       #region 多条件筛选
                ///查询数据库的仓储类
                ItestRepository test = new TestRepository();
                test.FindAll(d => d.Id == 1);
                Expression<Func<Test, bool>> pre;
                pre = s => s.Id == 1;
                int money = 10;
                if (money > 0)
                {
                    pre = pre.And(d => d.Money == money);
                }
    
                var vlus = test.FindAll(pre.Compile());
                #endregion
    

      

  • 相关阅读:
    Java之IO(一)InputStream和OutputStream
    bitset库
    assert
    C++ 与 Python 混合编程
    C++多线程
    C++11新特性
    C++性能优化指南
    C++随机数
    C++中struct与class的区别
    C++杂记
  • 原文地址:https://www.cnblogs.com/SHa-Jazy/p/15508939.html
Copyright © 2020-2023  润新知