标签:
业务类中 我们根据条件来动态的查询
创建IQueryable接口
public IQueryable<TEntity> GetQueryable() { IQueryable<TEntity> query = dbContext.Set<TEntity>(); return query; }
为什么创建IQueryable接口?IQueryable会在调用ToList()或者Count()等方法的时候,才回向数据库查询,所以只会向数据库提交一次带条件的查询语句,从而提升了性能。
public IList<Shop> Query(Shop model) { IQueryable<Shop> query = GetQueryable().Where(m => m.DelStatus == 0); if (model.ShopID != null) { query = query.Where(m => m.ShopID == model.ShopID); } if (!string.IsNullOrEmpty(model.Name)) { query = query.Where(m => m.Name.Contains(model.Name)); } if (!string.IsNullOrEmpty(model.Province)) { query = query.Where(m => m.Province == model.Province); } if (!string.IsNullOrEmpty(model.City)) { query = query.Where(m => m.City == model.City); } if (!string.IsNullOrEmpty(model.Area)) { query = query.Where(m => m.Area == model.Area); } query = query.OrderBy(m => m.ShopID); return query.ToList(); }
标签:
原文地址:http://www.cnblogs.com/njcxwz/p/5609892.html