1

hope someone can help. I have an object:

public class folder
{
    public string folderName { get; set; }
    public string folderPath { get; set; }
    public List<folder> subFolders { get; set; }
}

as you can see the object contains an object referencing itself allowing me to build up a list of folders and sub folders, I then have a

List<folder> mycustomlist ect..

to store this in. This list can be extremely deep and works fine when binding to a treeview or custom listview.

My problem is trying to remove a nested folder from the List, I can get the object but when using

mycustomlist.Remove(thefolder) 

it cant seem to locate the nested object to remove it.

I've tried several ways using Linq but with no joy, maybe their is a better way of doing this?

Hope someone can help,

Thanks

Nath

Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
NathanJ
  • 49
  • 10
  • The List doesn't know it's recursive. It does not, and cannot, know what properties its items have, or what they mean. Is `theFolder` in `mycustomlist`, or in a list belonging to an item in `mycustomlist` (or a child of a child, etc.)? If you want to remove items from anywhere in this tree structure you've created, you'll have to write your own recursive method to do that. – 15ee8f99-57ff-4f92-890c-b56153 Feb 12 '16 at 13:56
  • Why do you need to find some nested objects? Your list should contain top-level folders. – Sergey Berezovskiy Feb 12 '16 at 13:57
  • 1
    Find the object containing the list which has the `folder` object you want to delete, and then delete from that point... Not from the top level... – code4life Feb 12 '16 at 13:59
  • @EdPlunkett thanks, yes the thefolder exists in a child of a child. I was hoping it was clever enough but I guess I better get writing a recursive method! Thanks for the help :) – NathanJ Feb 12 '16 at 14:00
  • 1
    Declare its parent as a property so you can remove it from its parent. `thefolder.Parent.remove(thefolder)` – Hamid Feb 12 '16 at 14:10

3 Answers3

3

it cant seem to locate the nested object to remove it.

Remove is not recursive. It only removes an item directly in the list.

If thefolder is in a sublist then you'll need to recursively search for that and then remove it from the list it's actually in.

private bool RecursiveRemove(folder thisList, folder thefolder)
{
    if (thisList.Contains(theFolder))
    {
        thisList.Remove(theFolder);
        return true;
    }
    else
    {
        foreach (var folder in thisList.subFolders)
        {
            if (RecursiveRemove(folder, theFolder))
            {
                return true;
            }
        }
    }

    return false;  // not found
}
ChrisF
  • 134,786
  • 31
  • 255
  • 325
0

The List doesn't know it's recursive. It does not, and cannot, know what properties its items have, or what they mean. Is theFolder in mycustomlist, or in a list belonging to an item in mycustomlist (or a child of a child, etc.)?

If you want one function to remove a given item from anywhere in this tree structure you've created, regardless of which particular root or child list it lives in, you'll have to write your own recursive method to do that.

0

This is what I have got for this

    public void RemoveFolder(Folder folder, List<Folder> folderList)
    {
        if (folderList != null)
        {
            if (folderList.Contains(folder))
            {
                folderList.Remove(folder);
            }
            else
            {
                foreach (var subFolder in folderList.Select(x => x.subFolders))
                {
                    RemoveFolder(folder, subFolder);
                }

            }
        }
    }

But I think you will need to write your own equity comparer

See here List.Contains is not working as hoped

Community
  • 1
  • 1
testydonkey
  • 149
  • 4