// GET: Dish public ActionResult Index(Dish KeySearch, int?FromCost, int?ToCost, int page = 1, int pageSize = 10) { DishRequestModel model = new DishRequestModel(); model = _dishRepo.GetAllDish(KeySearch, FromCost, ToCost, page, pageSize); return(View(model)); }
public DishRequestModel GetAllDish(Dish KeySearch, int?FromCost, int?ToCost, int page, int pageSize) { DishRequestModel request = new DishRequestModel(); var query = from d in db.Dishes where d.IsDeleted == 0 select(d); request.page = page; request.pageSize = pageSize; int startRow = (page - 1) * pageSize; if (!string.IsNullOrEmpty(KeySearch.DishName)) { query.Where(x => x.DishCode.Contains(KeySearch.DishName) || x.DishName.Contains(KeySearch.DishName)); } if (KeySearch.SupplierId != null) { query.Where(x => x.SupplierId == KeySearch.SupplierId); } if (FromCost != null) { query.Where(x => x.Cost >= FromCost); } if (ToCost != null) { query.Where(x => x.Cost <= ToCost); } request.totalRecord = query.ToList().Count; var joinQuery = from d in query join s in db.Suppliers.Where(x => x.IsDeleted == 0) on d.SupplierId equals s.id into ls from ld in ls.DefaultIfEmpty() select new DishInfo() { id = d.id, DishCode = d.DishCode, DishName = d.DishName, SupplierId = d.SupplierId, SupplierCode = ld.SupplierCode, SupplierName = ld.SupplierName, Cost = d.Cost, Description = d.Description, Image = d.Image, JugmentPoint = d.JugmentPoint, JugmentQty = d.JugmentQty, RegisterQty = d.RegisterQty, }; request.data = joinQuery.ToList(); int totalPage = 0; if (request.totalRecord % pageSize == 0) { totalPage = request.totalRecord / pageSize; } else { totalPage = request.totalRecord / pageSize + 1; } request.totalPage = totalPage; request.KeySearch = KeySearch; return(request); }