Is it possible to convert a List to Tree so that when I delete parent node all child nodes will be deleted automatically?
Reason for this is that I have recursive categories and I want to show all categories except child categories (recursively) and easiest way in my mind is to do something like in the picture.
As you can see in this scenario when I delete red node all green nodes will remove themselves automatically. What are my options?
Here is the model I'm using if it makes any difference.
public class Category
{
public int Id { get; set; }
public int? RootCategoryId { get; set; }
public virtual Category RootCategory { get; set; }
public virtual ICollection<Category> ChildCategories { get; set; }
}
This is what I have so far
// This would eliminate only current category but not its children = bad
var availableCategories = _db.Categories.Where(x => x.Id != currentlyEditedId);
So for example when I want to edit a category I would call _db.Categories.Where(x => x.Id != currentlyEditedId);
. This would eliminate the current category (in the picture it would delete red one) however the green ones would stay.
How can I make sure that if I delete the red one the green ones will delete as well?
In the end I want to have a list with all black items from the picture.
Category
class you've just shown? – Servy