public ActionResult Menu() { DBContext db = new DBContext(); MenuViewModel menuViewModel = new MenuViewModel(); List<ItemFollowCategoryViewModel> itemFollowCategoryViewModels = new List<ItemFollowCategoryViewModel>(); ItemFollowCategoryViewModel itemFollowCategoryViewModel = new ItemFollowCategoryViewModel(); List<ItemViewModel> itemViewModels = new List<ItemViewModel>(); var query = from cat in db.Categories join item in db.Items.Where(i => i.IsDelete != true).OrderBy(i => i.CategoryId) on cat.CategoryId equals item.CategoryId select new { cat.CategoryId, cat.CategoryName, cat.Thumbnail, item.ItemId, item.ItemName, item.ItemInfo, item.ItemStatus, item.Prices, item.Promotions, item.ThumbnailPath }; int sumQUery = query.ToList().Count(); int cateIDRoot = 0; bool isNewCat = false; int count = 0; foreach (var item in query) { count++; if (cateIDRoot == 0)//moi dau { isNewCat = true; } else if (cateIDRoot != item.CategoryId) //truoc khi bat dau 1 category moi thi add cu, reset lai tu dau { //neu catID lan loop nay khac voi goc va != 0, => la 1 nhom item moi //add ItemViewModels vao itemFollowCategoryViewModel, itemFollowCategoryViewModel gio co du gia tri can roi, tiep tuc add no vao (list) itemFollowCategoryViewModels //reset lai itemViewModels = null; //reset lai itemFollowCategoryViewModel //gan 1 flag = true de itemFollowCategoryViewModel them info cua cat vao itemFollowCategoryViewModel.listItemViewModel = itemViewModels; itemFollowCategoryViewModels.Add(itemFollowCategoryViewModel); itemViewModels = new List<ItemViewModel>(); itemFollowCategoryViewModel = new ItemFollowCategoryViewModel(); isNewCat = true; } ItemViewModel itemViewModel = new ItemViewModel(); itemViewModel.ItemId = item.ItemId; itemViewModel.ItemName = item.ItemName; itemViewModel.ThumbnailPath = item.ThumbnailPath; ICollection<Price> listPrice = item.Prices; var value = listPrice.Where(l => l.IsDelete != true).FirstOrDefault(); if (value != null) { itemViewModel.Prices = value.UnitPrice; } itemViewModels.Add(itemViewModel); cateIDRoot = item.CategoryId;//gan catId goc = catID trong lan loop nay if (isNewCat)//chi add 1 lan category cua 1 nhom item { itemFollowCategoryViewModel.categoryViewModel = new CategoryViewModel(); itemFollowCategoryViewModel.categoryViewModel.Id = item.CategoryId; itemFollowCategoryViewModel.categoryViewModel.Name = item.CategoryName; itemFollowCategoryViewModel.categoryViewModel.Thumbnail = item.Thumbnail; isNewCat = false;//sau khi add xong thi set.false de ko add nua } if (count == sumQUery) //khi day da la vong cuoi cung { itemFollowCategoryViewModel.listItemViewModel = itemViewModels; itemFollowCategoryViewModels.Add(itemFollowCategoryViewModel); } } menuViewModel.listItemFollowCategoryViewModel = itemFollowCategoryViewModels; return View(menuViewModel); }
// GET: Items public ActionResult Index() { ItemAdminViewModel myItemListView = new ItemAdminViewModel(); myItemListView.MyItemListView = new List<ItemFollowCategoryViewModel>(); ItemFollowCategoryViewModel itemFollowCategoryViewModel = new ItemFollowCategoryViewModel(); List<ItemViewModel> itemViewModels = new List<ItemViewModel>(); var query = from cat in db.Categories join item in db.Items.Where(i => i.IsDelete != true) on cat.CategoryId equals item.CategoryId select new { cat.CategoryId, cat.CategoryName, cat.Thumbnail, item.ItemId, item.ItemName, item.ItemInfo, item.ItemStatus,item.IsActive, item.Prices, item.Promotions, item.ThumbnailPath }; int sumCat = 0; int cateIDRoot = 0; bool isNewCat = false; int totalCat = query.ToList().Count(); foreach (var item in query) { sumCat++; if (cateIDRoot == 0) { isNewCat = true; } else if (cateIDRoot != item.CategoryId) {//neu catID lan loop nay khac voi goc va != 0, => la 1 nhom item moi //add ItemViewModels vao itemFollowCategoryViewModel, itemFollowCategoryViewModel gio co du gia tri can roi, tiep tuc add no vao (list) itemFollowCategoryViewModels //reset lai itemViewModels = null; //reset lai itemFollowCategoryViewModel //gan 1 flag = true de itemFollowCategoryViewModel them info cua cat vao itemFollowCategoryViewModel.listItemViewModel = itemViewModels; myItemListView.MyItemListView.Add(itemFollowCategoryViewModel); itemViewModels = new List<ItemViewModel>(); itemFollowCategoryViewModel = new ItemFollowCategoryViewModel(); isNewCat = true; } ItemViewModel itemViewModel = new ItemViewModel(); itemViewModel.ItemId = item.ItemId; itemViewModel.ItemName = item.ItemName; itemViewModel.ItemInfo = item.ItemInfo; itemViewModel.ThumbnailPath = item.ThumbnailPath; itemViewModel.ItemStatus = item.ItemStatus; itemViewModel.IsActive = item.IsActive; ICollection<Price> listPrice = item.Prices; var value = listPrice.Where(l => l.IsDelete != true).FirstOrDefault(); if (value != null) { itemViewModel.Prices = value.UnitPrice; } itemViewModels.Add(itemViewModel); cateIDRoot = item.CategoryId;//gan catId goc = catID trong lan loop nay if (isNewCat)//chi add 1 lan category cua 1 nhom item { itemFollowCategoryViewModel.categoryViewModel = new CategoryViewModel(); itemFollowCategoryViewModel.categoryViewModel.Id = item.CategoryId; itemFollowCategoryViewModel.categoryViewModel.Name = item.CategoryName; itemFollowCategoryViewModel.categoryViewModel.Thumbnail= item.Thumbnail; isNewCat = false;//sau khi add xong thi set.false de ko add nua } if (sumCat == totalCat) { itemFollowCategoryViewModel.listItemViewModel = itemViewModels; myItemListView.MyItemListView.Add(itemFollowCategoryViewModel); } } return View(myItemListView); }
public ActionResult DoMenu(int? id) { DBContext db = new DBContext(); DoMenuViewModel doMenu = new DoMenuViewModel(); doMenu.itemFollowCategoryViewModels = new ItemFollowCategoryViewModel(); doMenu.itemFollowCategoryViewModels.categoryViewModel = new CategoryViewModel(); var queryCat = from cat in db.Categories join item in db.Items on cat.CategoryId equals item.CategoryId where item.IsDelete != true group cat by item.CategoryId into g select new { g.Key, g.FirstOrDefault().CategoryName, g.FirstOrDefault().Thumbnail }; List<Category> listCategory = db.Categories.ToList(); List<CategoryViewModel> listCategoryViewModel = new List<CategoryViewModel>(); CategoryViewModel category; foreach (var item in queryCat) { category = new CategoryViewModel(); category.Id = item.Key; category.Name = item.CategoryName; category.Thumbnail = item.Thumbnail; listCategoryViewModel.Add(category); if (category.Id == id) { doMenu.itemFollowCategoryViewModels.categoryViewModel = category; } } List<ItemViewModel> itemViewModels = new List<ItemViewModel>(); ItemViewModel itemViewModel; var queryItem = from item in db.Items where item.IsDelete != true && item.CategoryId == id select item; foreach (var item in queryItem) { itemViewModel = new ItemViewModel(); itemViewModel.ItemId = item.ItemId; itemViewModel.ItemName = item.ItemName; itemViewModel.ItemInfo = item.ItemInfo; if (item.Prices.ToList().Count != 0) { ICollection<Price> listPrice = item.Prices; var value = listPrice.Where(p => p.IsDelete != true).FirstOrDefault(); itemViewModel.Prices = value.UnitPrice; } itemViewModel.ThumbnailPath = item.ThumbnailPath; itemViewModels.Add(itemViewModel); } //cho menu o tren doMenu.categoryViewModels = listCategoryViewModel; //cho danh sach cac item cua menu voi id can tim doMenu.itemFollowCategoryViewModels.listItemViewModel = itemViewModels; return View(doMenu); }