public virtual T GetsingleByCondition(Expression <Func <T, bool> > where, string[] includes = null) { if (includes != null && includes.Count() > 0) { var query = dbContext.Set <T>().Include(includes.First()); foreach (var include in includes.Skip(1)) { query = query.Include(include); } return(query.FirstOrDefault(where)); } return(dbContext.Set <T>().FirstOrDefault(where)); }
public virtual IEnumerable <Shop> GetMultiPagingForShop(Expression <Func <Shop, bool> > predicate, out int total, int curPage = 1, int pageSize = 20, string sort = "", string type = "") { int skipCount = (curPage - 1) * pageSize; IQueryable <Shop> _resetSet; _resetSet = predicate != null?dbContext.Set <Shop>().Where <Shop>(predicate).AsQueryable() : dbContext.Set <Shop>().AsQueryable(); switch (sort) { case "ID": if (type == "asc") { _resetSet = skipCount == 0 ? _resetSet.OrderBy(s => s.ID).Take(pageSize) : _resetSet.OrderBy(s => s.ID).Skip(skipCount).Take(pageSize); } else // type = default hoặc type = desc { _resetSet = skipCount == 0 ? _resetSet.OrderByDescending(s => s.ID).Take(pageSize) : _resetSet.OrderByDescending(s => s.ID).Skip(skipCount).Take(pageSize); } break; case "Name": if (type == "asc") { _resetSet = skipCount == 0 ? _resetSet.OrderBy(s => s.Name).Take(pageSize) : _resetSet.OrderBy(s => s.Name).Skip(skipCount).Take(pageSize); } else { _resetSet = skipCount == 0 ? _resetSet.OrderByDescending(s => s.Name).Take(pageSize) : _resetSet.OrderByDescending(s => s.Name).Skip(skipCount).Take(pageSize); } break; case "Address": if (type == "asc") { _resetSet = skipCount == 0 ? _resetSet.OrderBy(s => s.DetailAddress).Take(pageSize) : _resetSet.OrderBy(s => s.DetailAddress).Skip(skipCount).Take(pageSize); } else { _resetSet = skipCount == 0 ? _resetSet.OrderByDescending(s => s.DetailAddress).Take(pageSize) : _resetSet.OrderByDescending(s => s.DetailAddress).Skip(skipCount).Take(pageSize); } break; case "Description": if (type == "asc") { _resetSet = skipCount == 0 ? _resetSet.OrderBy(s => s.Description).Take(pageSize) : _resetSet.OrderBy(s => s.Description).Skip(skipCount).Take(pageSize); } else { _resetSet = skipCount == 0 ? _resetSet.OrderByDescending(s => s.Description).Take(pageSize) : _resetSet.OrderByDescending(s => s.Description).Skip(skipCount).Take(pageSize); } break; default: break; } total = _resetSet.Count(); return(_resetSet.AsQueryable()); }