Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7926875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:59:59+00:00 2026-06-03T18:59:59+00:00

I have a collection of NodeObject classes in a hierarchical list. The list can

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T19:00:19+00:00Added an answer on June 3, 2026 at 7:00 pm

    Here a recursive way of doing this:

    private void DeleteNode(IList<Node> nodes, Guid id)
    {
        Node nodeToDelete = null;
        foreach (var node in nodes)
        {
            if (node.Id == id)
            {
                nodeToDelete = node;
                break;
            }
            DeleteNode(node.Children, id);
        }
        if (nodeToDelete != null)
        {
            nodes.Remove(nodeToDelete);
        }
    }
    

    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.

    private void DeleteNode(IList<Node> nodes, int id)
    {
        for (var index = 0; index < nodes.Count; index++)
        {
            var currentNode = nodes[index];
            if (currentNode.Id == id)
            {
                nodes.Remove(currentNode);
                break;
            }
            DeleteNode(currentNode.Children, id);
        }
    }
    

    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:

    private void DeleteNode(IList<Node> flatNodes, Guid id)
    {
        var nodeToDelete = flatNodes.FirstOrDefault(n => n.Id == id);
        if (nodeToDelete != null)
        {
            var parent = flatNodes.First(n => n.Id == nodeToDelete.ParentId);
            parent.Children.Remove(nodeToDelete);
        }
    }
    
    private void DeleteNodeFromFlatDictionary(IDictionary<Guid, Node> flatNodes, Guid id)
    {
        if (!flatNodes.ContainsKey(id)) return;
        var nodeToDelete = flatNodes[id];
        flatNodes[nodeToDelete.ParentId].Children.Remove(id);
    }
    

    If you want the UI to recognize changes you need to use ObservableCollection<Node>, though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have collection: class HashedData : IComparable { string key; public string Key {
I have a collection of classes that inherit from an abstract class I created.
Hello guys i have collection : public class ActionData { private int iD; public
I have Collection List<Car> . How to compare each item from this collection with
I have a collection of the form: map<key, list<object> > I only ever insert
I have a collection like this List<int> {1,15,17,8,3}; how to get a flat string
I have class Person: public class Person : INotifyPropertyChanged { private String name =
Assuming I have an Entity Framework 4.2 class like this: class Company { public
I have the following class: class Item { public string[] field1; public int field2;
I have collection of Questions as List. For each question I want to show

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.