1

我有一个具有以下结构的分层对象:

public class Folder
{
    public Folder Parent { get; set; }
    public IList<Folder> Child { get; set; }
}

如果文件夹是根文件夹,则父文件夹为null. 如果文件夹不是根目录,则父文件夹是not null.

我需要找到一个文件夹的最终父级,这意味着根文件夹 ( not null) 如果存在的话。

如果可能的话,我会参考避免 while 循环。如果可能的话,我想使用 Linq 表达式来完成它。

4

1 回答 1

11

如果它是最快的方法,为什么要避免 while 循环?

Folder root = myFolder;
while(root.Parent != null) root = myFolder.Parent;
于 2011-02-28T17:41:48.763 回答