category = allCategories != null ? allCategories.FirstOrDefault(c => c.Id == c.ParentCategoryId)
it must be like this:
category = allCategories != null ? allCategories.FirstOrDefault(c => c.Id == category.ParentCategoryId)
here is the current full code:
public virtual IList<Category> GetCategoryBreadCrumb(Category category, IList<Category> allCategories = null, bool showHidden = false)
{
if (category == null)
throw new ArgumentNullException(nameof(category));
var result = new List<Category>();
//used to prevent circular references
var alreadyProcessedCategoryIds = new List<int>();
while (category != null && //not null
!category.Deleted && //not deleted
(showHidden || category.Published) && //published
(showHidden || _aclService.Authorize(category)) && //ACL
(showHidden || _storeMappingService.Authorize(category)) && //Store mapping
!alreadyProcessedCategoryIds.Contains(category.Id)) //prevent circular references
{
result.Add(category);
alreadyProcessedCategoryIds.Add(category.Id);
category = allCategories != null ? allCategories.FirstOrDefault(c => c.Id == category.ParentCategoryId)
: this.GetCategoryById(category.ParentCategoryId);
}
result.Reverse();
return result;
}