模糊查询+分页

 /// <summary>
        /// 查询员工
        /// </summary>
        /// <param name="queryString">待查询的字符串</param>
        /// <returns></returns>
        public static IList<Xlzt.DataAccessLayer.Member> 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;
            }

        }

 

posted @ 2012-07-09 22:43  Alec-Yin  阅读(2587)  评论(0编辑  收藏  举报
更多前端博客,访问:http://lhyin.com