模糊查询+分页

 /// 
        /// 查询员工
        /// 
        /// 待查询的字符串
        /// 
        public static IList QueryMember(out int totleCount, int pageNum, int pageSize,String queryString)
        {
            try
            {
                using (Xlzt.DataAccessLayer.CRM_DBEntity db = new CRM_DBEntity())
                {
                    //按员工姓名,工号,部门三个字段模糊查询
                    var result = db.Members.OrderBy(c=>c.account).Where(obj => obj.name.StartsWith(queryString) || obj.name.EndsWith(queryString) || obj.name.IndexOf(queryString) != -1
                       || obj.account.StartsWith(queryString) || obj.account.EndsWith(queryString) || obj.account.IndexOf(queryString) != -1
                         || obj.department.StartsWith(queryString) || obj.department.EndsWith(queryString) || obj.department.IndexOf(queryString) != -1
                        );
                    //获取总记录数目
                    totleCount = result.ToArray().Length;
                    //获取请求页的最大索引
                    int max = (result.ToArray().Length <= pageNum * pageSize) ? result.ToArray().Length : pageNum * pageSize;
                    if (max < (pageNum - 1) * pageSize)
                    {
                        //请求页的最大索引比它上一页的最大索引还小:矛盾
                        return null;
                    }
                    else if (max < pageSize * pageNum)
                    {
                        //请求页是左后一页
                        return result.Skip((pageNum - 1) * pageSize).ToList();//使用Skip时要使用OrderBy
                    }
                    else
                    {
                        //请求页正常返回
                        return result.Skip((pageNum - 1) * pageSize).Take(pageSize).ToList();
                    }
                   
                }
            }
            catch (Exception)
            {
                totleCount = 0;
                return null;
            }

        }

 

转载于:https://www.cnblogs.com/yinluhui0229/archive/2012/07/09/2583796.html

你可能感兴趣的:(模糊查询+分页)