I'am trying with EF Core to get nested categories. Problem is that it's so slow.
Look at my code:
public async Task<List<Category>> GetNestedCategoryAsync(Category category)
{
if (category is null)
return new List<Category>();
IQueryable<Category> query = db.Categories.Include(x => x.Childs).Include(x => x.Products).Where(x => category.Childs.Contains(x)).AsNoTracking();
List<Category> nestedCategories = await query.ToListAsync();
foreach (Category nestedCategory in nestedCategories.ToArray())
nestedCategories.AddRange(await GetNestedCategoryAsync(nestedCategory));
return nestedCategories;
}
Actually I don't know how to translate this SQL into EF.. is it even possible? Its thousand times faster
With Categories_CTE As
(
Select *
From Categories
Where Id = 8692
Union All
Select t.*
From Categories t
Inner Join Categories_CTE c On c.Id = t.ParentId
)
Select c.*
From Categories_CTE c;
Thanks for any tips