English 中文(简体)
回收从树木中删除物品
原标题:Recursively remove items from Tree

我有一个树图,其结构如下:

var myTree = myRepository.GetTree();

标的<> />以下列方式编排:

public class Folder
{
   public string Name { get; set; }
   public List<Folder> Folders { get; set; }
   public Folder Parent { get; set; }
}

Assuming that a repository is returning to me something like this structure:

MyFolder
   -MyChild01
   -MyChild02
      -MySubChild01       
   -MyChild03
      -MySubChild02

如果该项目的名称为02,我需要重新阅读树木中的所有项目。 我必须把该项目从树中删除。 因此,最后结果应当是:

MyFolder
   -MyChild01
   -MyChild03

我愿看到,如何利用以匿名代表身份或以休养身份行事的解职准则来做到这一点。

最佳回答

There s a number of approaches you could take to do this.

最容易做的事情是采取功能方法和重建树木,重新排除与你的模式相匹配的夹。

static Folder Filtered(Folder root, Func<Folder, bool> predicate)
{
    return new Folder
    {
        Name = root.Name,
        Parent = root.Parent,
        Folders = root.Folders
            .Where(predicate)
            .Select(subFolder => Filtered(subFolder, predicate))
            .ToList(),
    };
}

Folder myTree = ...;
var filtered = Filtered(myTree, f => f.Name.Contains("02"));

无论出于何种原因,如果你不提出新事例,而是修改现有的事例,就必须作一些调整,但同样简单。

static Folder Filtered(Folder root, Func<Folder, bool> predicate)
{
    // make use of the RemoveAll() method for lists
    root.Folders.RemoveAll(subFolder => !predicate(subFolder));
    foreach (var subFolder in root.Folders)
    {
        Filtered(subFolder, predicate);
    }
    return root;
}
问题回答
    static void Main(string[] args)
    {
        var root = new Folder()
        {
            Name = "MyFolder",
            Folders = new List<Folder>(){
                    new Folder() {Name="MyChild01", Folders = new List<Folder>()},
                    new Folder() {Name="MyChild02", Folders = new List<Folder>(){
                        new Folder() { Name="MySubChild01", Folders = new List<Folder>()}}
                    },
                    new Folder() {Name="MyChild03", Folders = new List<Folder>(){
                        new Folder() { Name="MySubChild02", Folders = new List<Folder>()}}
                    },
                }
        };

        var filtered = applyFilter(root, f=>!f.Name.EndsWith("02"));

    }

    static Folder applyFilter( Folder root, Predicate<Folder> filter )
    {
        var result = new Folder() { Name = root.Name, Parent = root.Parent };
        result.Folders = (from child in root.Folders
                         where filter(child)
                         select applyFilter(child, filter)).ToList();
        return result;
    }




相关问题
Recursive same-table query in SQL Server 2008

I have the following table in a SQL Server 2008 database: Id Name ParentFolder -- ---- ------------ 1 Europe NULL 2 Asia NULL 3 Germany 1 4 UK 1 5 China ...

Finding a class within list

I have a class (Node) which has a property of SubNodes which is a List of the Node class I have a list of Nodes (of which each Node may or may not have a list of SubNodes within itself) I need to be ...

Selecting records during recursive stored procedure

I ve got a content management system that contains a hierarchical structure of categories, with sub-categories subject to different ordering options at each level. Currently, that s retrieved by a (...

热门标签