public List <CategoryNavViewModel> GetCategoriesAndSubCategories() { try { List <CategoryNavViewModel> categoryNavViewModels = new List <CategoryNavViewModel>(); var categoryNavs = _context.CategorySubCategoryTable.FromSql(@" Select c.CategoryId,c.CategoryName ,ISNULL(sc.SubCategoryOneId,0) AS SubCategoryOneId,ISNULL(sc.SubCategoryName,'') AS SubCategoryName,count(pkproductid) as ProductsCount From Category c left join SubCategoryOne sc on c.CategoryId = sc.CategoryId left join Product p on sc.SubCategoryOneId = p.SubCategoryOneId group by c.CategoryId,c.CategoryName,sc.SubCategoryOneId,sc.SubCategoryName order by c.CategoryId ").ToList(); //Added by Ashok List <string> categoryList = categoryNavs.Select(x => x.CategoryName).Distinct().ToList(); foreach (string item in categoryList) { CategoryNavViewModel categoryNav = categoryNavs.Where(x => x.CategoryName == item).Select(x => new CategoryNavViewModel { CategoryId = x.CategoryId, CategoryName = x.CategoryName }).FirstOrDefault(); List <SubCategoriesNavViewModel> subCatList = categoryNavs.Where(x => x.CategoryName == item).Select(x => new SubCategoriesNavViewModel { SubCategoryId = x.SubCategoryOneId, SubCategoryName = x.SubCategoryName, NumberOfProducts = x.ProductsCount }).ToList(); categoryNav.SubCategories = subCatList; categoryNavViewModels.Add(categoryNav); } //end by Ashok //foreach (var row in categoryNavs) //{ // CategoryNavViewModel categoryNav = new CategoryNavViewModel(); // categoryNav.CategoryId = row.CategoryId; // categoryNav.CategoryName = row.CategoryName; // List<SubCategoriesNavViewModel> subCatList=new List<SubCategoriesNavViewModel>(); // categoryNav.SubCategories = subCatList; //} return(categoryNavViewModels); } catch (Exception e) { throw; } }
/// <summary> /// 后台航的数据源 /// </summary> /// <returns></returns> public List <CategoryNavViewModel> GetCategoryNav() { var categorys = categoryRepository.GetCategory(); var subCategorys = categoryRepository.GetSubCategoryWithArticleNum(); var noSubCategorys = categoryRepository.GetCategoryWithArticleNum(); List <CategoryNavViewModel> models = new List <CategoryNavViewModel>(); foreach (var category in categorys) { CategoryNavViewModel model = new CategoryNavViewModel(); model.categoryId = category.CategoryId; model.CategoryName = category.CategoryName; List <SubCategoryNavViewModel> SubCategory = new List <SubCategoryNavViewModel>(); var thisSubs = subCategorys.Where(x => x.CategoryId == category.CategoryId); //未分类的数据 var noSub = noSubCategorys.Where(x => x.CategoryId == category.CategoryId).First(); SubCategoryNavViewModel sub = new SubCategoryNavViewModel(); sub.SubCategoryId = noSub.SubCategoryId; sub.CategoryId = noSub.CategoryId; sub.SubCategoryName = noSub.SubCategoryName; sub.ArticleNum = noSub.ArticleNum; SubCategory.Add(sub); //有分类的数据 foreach (var thissub in thisSubs) { SubCategory.Add(new SubCategoryNavViewModel() { SubCategoryId = thissub.SubCategoryId, CategoryId = thissub.CategoryId, SubCategoryName = thissub.SubCategoryName, ArticleNum = thissub.ArticleNum }); } model.SubCategory = SubCategory; models.Add(model); } return(models); }
/// <summary> /// 前台导航的数据源 /// 没有文章不显示,没有子类显示为分类名称 /// </summary> /// <returns></returns> public List <CategoryNavViewModel> GetMenu() { var categorys = categoryRepository.GetCategory(); var subCategorys = categoryRepository.GetSubCategoryWithArticleNum(); var noSubCategorys = categoryRepository.GetCategoryWithArticleNum(); List <CategoryNavViewModel> models = new List <CategoryNavViewModel>(); foreach (var category in categorys) { CategoryNavViewModel model = new CategoryNavViewModel(); model.categoryId = category.CategoryId; model.CategoryName = category.CategoryName; List <SubCategoryNavViewModel> SubCategory = new List <SubCategoryNavViewModel>(); if ("导航".Equals(category.CategoryName)) { SubCategory.Add(new SubCategoryNavViewModel() { SubCategoryId = "0", CategoryId = "0", SubCategoryName = "首页", ArticleNum = 1, Url = "/Home/Index" }); } var thisSubs = subCategorys.Where(x => x.CategoryId == category.CategoryId);//子类 var noSub = noSubCategorys.Where(x => x.CategoryId == category.CategoryId).First(); if (thisSubs.Select(x => x.ArticleNum).Sum() != 0 || noSub.ArticleNum != 0) { if (thisSubs.Count() == 0) { SubCategory.Add(new SubCategoryNavViewModel() { SubCategoryId = noSub.SubCategoryId, CategoryId = noSub.CategoryId, SubCategoryName = category.CategoryName, ArticleNum = noSub.ArticleNum, Url = string.Format("/Home/List/{0}/{1}.html", noSub.CategoryId, noSub.SubCategoryId) }); } //有分类的数据 foreach (var thissub in thisSubs) { if (thissub.ArticleNum > 1) { SubCategory.Add(new SubCategoryNavViewModel() { SubCategoryId = thissub.SubCategoryId, CategoryId = thissub.CategoryId, SubCategoryName = thissub.SubCategoryName, ArticleNum = thissub.ArticleNum, Url = string.Format("/Home/List/{0}/{1}.html", thissub.CategoryId, thissub.SubCategoryId) }); } else if (thissub.ArticleNum == 1) { SubCategory.Add(new SubCategoryNavViewModel() { SubCategoryId = thissub.SubCategoryId, CategoryId = thissub.CategoryId, SubCategoryName = thissub.SubCategoryName, ArticleNum = thissub.ArticleNum, Url = string.Format("/Home/Article/{0}", thissub.ArticleId) }); } } model.SubCategory = SubCategory; models.Add(model); } } return(models); }