基本思想:首先将数据全部查出来,然后根据搜索条件去除数据
代码实例:
//将数据库全部数据查出
var temp = db.KNOWLEDGE.AsNoTracking().Where(u => true);
//是否包含查询模块
if (!string.IsNullOrEmpty(moduleId))
{
decimal moduId =Convert.ToDecimal(moduleId);
temp =temp.Where(u => u.MODULE_ID==moduId);
}
//是否查询类别
if (!string.IsNullOrEmpty(categoryId))
{
int category_Id =Convert.ToInt32(categoryId);
CATEGORY category =CategoryDal.GetCategory(category_Id);
string fids = category.FIDS+","+categoryId;
decimal cateId =Convert.ToDecimal(categoryId);
temp =temp.Where(u => u.CATEGORY_ID ==cateId||u.CATEGORY.FIDS.ToUpper().Contains(fids.ToUpper()));
}
//是否查询标题
if(!string.IsNullOrEmpty(knowTitle))
{
temp =temp.Where(u => u.KNOWLEDGE_TITLE != null &&u.KNOWLEDGE_TITLE.ToUpper().Contains(knowTitle.ToUpper()));
}
//对结果集进行分页查询
var knowledgeList = temp.Where(u =>true).OrderByDescending(u =>u.KNOWLEDGE_ID).Skip((pageIndex - 1) *pageSize).Take(pageSize);
//对结果集总记录数进行统计
count = temp.Count();
List list = knowledgeList.ToList();
return list;