linq 多条件查询

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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>() { new Person { Id=1,Name="chm1", Age=1, Birthday=Convert.ToDateTime("1991-01-01") }, new Person { Id=1,Name="chm2", Age=2, Birthday=Convert.ToDateTime("1992-01-01")}, new Person { Id=1,Name="chm3", Age=3, 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)); where = where.Or(p => p.Age.Equals(2)); string strUserName = "chm1"; where = where.And(p => p.Name.Contains(strUserName)); where = where.Or(p => p.Age.Equals(3)); var reulst = listPerson.AsQueryable().Where(where).ToList(); } private void testList_False() { List<Person> listPerson = new List<Person>() { new Person { Id=1,Name="chm1", Age=1, Birthday=Convert.ToDateTime("1991-01-01") }, new Person { Id=1,Name="chm2", Age=2, Birthday=Convert.ToDateTime("1992-01-01")}, new Person { Id=1,Name="chm3", Age=3, Birthday=Convert.ToDateTime("1993-01-01")} }; var where = PredicateBuilder.False<Person>(); string strUserName = "chm1"; where = where.And(p => p.Name.Contains(strUserName)); where = where.Or(p => p.Age.Equals(2)); 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 多条件查询的主要内容,如果未能解决你的问题,请参考以下文章

多条件动态LINQ 组合查询

asp.net(MVC) linq语句多条件查询

Linq to Entity 多条件 OR查询

LINQ多条件OR模糊查询

Linq动态条件

LINQ 查询条件外部参数