码迷,mamicode.com
首页 > 其他好文 > 详细

linq 多条件查询

时间:2016-09-29 17:32:48      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

 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 多条件查询

标签:

原文地址:http://www.cnblogs.com/caohuimingfa/p/5920444.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!