Esempio n. 1
0
        // 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);
        }
Esempio n. 2
0
        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);
        }