private static void Traverse(CategoryClass category, ref ShowLookUpObject lookupObj, ref List<ShowLookUpObject> slo) { if (category is Show) { if (category.CategoryId == lookupObj.SubCategoryId) { lookupObj.SubCategoryId = lookupObj.MainCategoryId; lookupObj.SubCategory = lookupObj.MainCategory; } lookupObj.ShowId = category.CategoryId; lookupObj.Show = category.CategoryName; slo.Add(lookupObj); } else { var subcategories = category.CategoryClassSubCategories.Select(s => s.SubCategory).Where(s => s.StatusId == 1); foreach (CategoryClass c in subcategories) { ShowLookUpObject lookupObj_t = new ShowLookUpObject(); lookupObj_t.MainCategoryId = lookupObj.MainCategoryId; lookupObj_t.MainCategory = lookupObj.MainCategory; var sub_category = category.CategoryClassSubCategories.Where(sc => sc.CategoryId == c.CategoryId).Single(); lookupObj_t.SubCategoryId = (lookupObj.SubCategoryId == null) ? sub_category.SubCategory.CategoryId : lookupObj.SubCategoryId; lookupObj_t.SubCategory = (lookupObj.SubCategory == null) ? sub_category.SubCategory.CategoryName : lookupObj.SubCategory; Traverse(c, ref lookupObj_t, ref slo); } } }
public SortedSet<int> GetAllParentCategories(CategoryClass category, TimeSpan? CacheDuration = null) { SortedSet<int> parentCategoryList = null; var cache = DataCache.Cache; string cacheKey = "GAParentCat:Cat:" + category.CategoryId; parentCategoryList = (SortedSet<int>)cache[cacheKey]; if (parentCategoryList == null) { parentCategoryList = new SortedSet<int>(); var parentCategories = category.CategoryClassParentCategories; var categoryParentIds = parentCategories.Select(c => c.ParentId); parentCategoryList.UnionWith(categoryParentIds); foreach (var item in parentCategories) { var categoryIds = GetAllParentCategories(item.ParentCategory, CacheDuration); if (categoryIds != null) parentCategoryList.UnionWith(categoryIds); } cache.Put(cacheKey, parentCategoryList, CacheDuration != null ? (TimeSpan)CacheDuration : DataCache.CacheDuration); } return (parentCategoryList); }