private List <Category> GetRecursiveCats(ref CatWithSubs cur) { var result = new List <Category>(); var subs = new List <CatWithSubs>(); var curSubCats = GetSubCategories(cur.Cat.Id); if (curSubCats.Any()) { foreach (var cat in curSubCats) { result.Add(cat); var tmp = new CatWithSubs() { Cat = cat }; result.AddRange(GetRecursiveCats(ref tmp)); var tmpClone = new CatWithSubs() { Cat = tmp.Cat, Subs = tmp.Subs }; subs.Add(tmpClone); } } cur.Subs = subs; return(result); }
public IEnumerable <CatWithSubs> GetAllCategories() { var mainCatWithSubs = new CatWithSubs() { Cat = Categories.FirstOrDefault(x => x.BaseCategory == null) }; GetRecursiveCats(ref mainCatWithSubs); _logger.LogInformation($"Got all categories. Root is {mainCatWithSubs.Cat.Name}"); return(mainCatWithSubs.Subs); }
private IQueryable <Product> FilterByCategory(IQueryable <Product> products, Guid categoryId) { var curCat = new CatWithSubs() { Cat = cats.FirstOrDefault(x => x.Id == categoryId) }; var apropriateCats = GetRecursiveCats(ref curCat); apropriateCats.Add(curCat.Cat); return(products.Where(p => apropriateCats.Contains(p.Category))); }
/// <summary> /// Gets categories in recursive way. /// </summary> /// <param name="cur">The current category with subs. It will be filled with Subcategories</param> private void GetRecursiveCats(ref CatWithSubs cur) { var subs = new List <CatWithSubs>(); var curSubCats = GetSubCategories(cur.Cat.Id); if (curSubCats.Any()) { foreach (var cat in curSubCats) { var tmp = new CatWithSubs() { Cat = cat }; GetRecursiveCats(ref tmp); var tmpClone = new CatWithSubs() { Cat = tmp.Cat, Subs = tmp.Subs }; subs.Add(tmpClone); } } cur.Subs = subs; }