linq 多条件查询

Linq 进行多条件查询的时候使用PredicateBuilder帮助类可以很好的解决。

类的源码:

 public static class PredicateBuilder
    {

        /// <summary>
        /// 应用True时:单个AND有效,多个AND有效;单个OR无效,多个OR无效;混合使用时写在AND后的OR有效
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static Expression<Func<T, bool>> True<T>() { return f => true; }

        /// <summary>
        /// 应用False时:单个AND无效,多个AND无效;单个OR有效,多个OR有效;混合使用时写在OR后面的AND有效
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        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);
        }
    }
测试方法:   private void testList_True()
        {
            List<Person> listPerson = new List<Person>()
            {
                ,Name=, Birthday=Convert.ToDateTime("1991-01-01") },
                ,Name=, Birthday=Convert.ToDateTime("1992-01-01")},
                ,Name=, Birthday=Convert.ToDateTime("1993-01-01")}
            };
            var where = PredicateBuilder.True<Person>();
            //var  where= PredicateBuilder_Object.True<Person>();

            //string strUserName = "chm1";
            //where = where.And(p => p.Name.Contains(strUserName));

            ));

            string strUserName = "chm1";
            where = where.And(p => p.Name.Contains(strUserName));
            ));
            var reulst = listPerson.AsQueryable().Where(where).ToList();
        }
        private void testList_False()
        {
            List<Person> listPerson = new List<Person>()
            {
                ,Name=, Birthday=Convert.ToDateTime("1991-01-01") },
                ,Name=, Birthday=Convert.ToDateTime("1992-01-01")},
                ,Name=, Birthday=Convert.ToDateTime("1993-01-01")}
            };
            var where = PredicateBuilder.False<Person>();

            string strUserName = "chm1";
            where = where.And(p => p.Name.Contains(strUserName));

            ));
            string strUserName2 = "chm3";
            where = where.And(p => p.Name.Contains(strUserName2));

            var reulst = listPerson.AsQueryable().Where(where).ToList();
        }

测试结果:

true: and (v)   and (v)  or(v)

true: or(x)   and (v)  or(v)

============================

false: or(v)     or(v) 

false: and (x)   and (x)

false: and (x)   or(v)  and(v)

上一篇:Linq in条件查询


下一篇:特许金融分析师 (CFA) 持证人现在一般在做什么工作?职业分布是怎样的?