Linq动态查询与模糊查询

IList<Department> mDepartmentLst = new List<Department>();
            IList<Employee> mEmployeeLst = new List<Employee>();
            mDepartmentLst.Add(new Department() { Id = 1, DeptNo = "001", DeptName = "开发部", IsBusiness = false });
            mDepartmentLst.Add(new Department() { Id = 2, DeptNo = "002", DeptName = "B2C 商务部", IsBusiness = true });
            mDepartmentLst.Add(new Department() { Id = 3, DeptNo = "003", DeptName = "B2B 商务部", IsBusiness = true });
            mDepartmentLst.Add(new Department() { Id = 4, DeptNo = "004", DeptName = "人事部", IsBusiness = false });

            mEmployeeLst.Add(new Employee() { Id = 1, DeptId = 1, EmpNo = "NO001", EmpName = "xushengtao" });
            mEmployeeLst.Add(new Employee() { Id = 1, DeptId = 1, EmpNo = "NO002", EmpName = "fankaijian" });
            mEmployeeLst.Add(new Employee() { Id = 1, DeptId = 4, EmpNo = "NO003", EmpName = "wangguoqin" });

            int mPageSize = 2;
            int mTotalCount = 0;
            Expression mWhereExpression;          // Filter clause (查询语句)
            // Get current request page
            string mCurPage = "1"; ;

            // Linq param which represent the search object (查询对象代替符)
            ParameterExpression mLinqParam = Expression.Parameter(typeof(Department), "p"); 
            //begin 处理“IsBusiness=true”
            //创建一个表示访问属性的 MemberExpression
            MemberExpression memberExpression = Expression.Property(mLinqParam, typeof(Department).GetProperty("IsBusiness"));
            ConstantExpression constantExpression = Expression.Constant(false);
            mWhereExpression = Expression.Equal(memberExpression, constantExpression);
            //end

            string mDeptName = "人事部";
            if (!String.IsNullOrEmpty(mDeptName))
            {
                mWhereExpression = Expression.And(mWhereExpression, Expression.Call(Expression.Property(mLinqParam,
                    typeof(Department).GetProperty("DeptName")), typeof(String).GetMethod("Contains"),
                    new Expression[] { Expression.Constant(mDeptName) }));
            }
            var mLambdaWhere = (Expression<Func<Department, bool>>)Expression.Lambda<Func<Department, bool>>(mWhereExpression,
                new ParameterExpression[] { mLinqParam });

            Func<Department, String> mLambdaOrder = p => p.DeptNo;
            mTotalCount = mDepartmentLst.AsQueryable().Where(mLambdaWhere).Count();
            var list = from o in mDepartmentLst.AsQueryable().Where(mLambdaWhere).
                           OrderBy(mLambdaOrder).Skip((int.Parse(mCurPage) - 1) * mPageSize).Take(mPageSize)
                       join c in mEmployeeLst on o.Id equals c.DeptId
                       select new
                       {
                           o.DeptNo,
                           o.DeptName,
                           o.IsBusiness,
                           c.DeptId,
                           c.EmpNo,
                           c.EmpName
                       };
posted @ 2012-10-22 16:02  xust  阅读(211)  评论(0)    收藏  举报