I have a collection of NodeObject classes in a hierarchical list. The list can be any number of levels deep.
public class NodeModel : ViewModelBase
{
public Guid Id { get; set; }
public string Caption { get; set; }
public string Description { get; set; }
public NodeType Type { get; set; }
public List<NodeModel> Children { get; set; }
}
How can I remove an item from the list using its Guid Id regardless of where it is in the list?
Here a recursive way of doing this:
If you’d like to have all the operations in one loop, do it with a for loop. In my opinion it’s much harder to read, though.
Another method would be to have a flat (non-hierarchical) list or even dictionary (quickest way!), which contains all the elements. You could add another property, which contains the Parent ID of the child. In some cases, especially when you have deep trees with lots of items, this way would be much more performant. If you want to remove a certain item, do it like this:
If you want the UI to recognize changes you need to use
ObservableCollection<Node>, though.