给定以下类,用于构建树层次结构:
public class simpletest
{
public simpletest Parent { get; set; }
public IList<simpletest> Children { get; set; }
public string Name { get; set; }
}
如何获取特定级别的所有对象的列表?
这是一些示例数据(希望其格式正确):
var root = new simpletest() { Name = "Root" };
var level1childA = new simpletest() { Name = "level1childA", Parent = root };
var level1childB = new simpletest() { Name = "level1childB", Parent = root };
var level2childA = new simpletest() { Name = "level2childA", Parent = level1childA };
var level2childB = new simpletest() { Name = "level2childB", Parent = level1childA };
var level2childC = new simpletest() { Name = "level2childC", Parent = level1childB };
var level2childD = new simpletest() { Name = "level3childD", Parent = level1childB };
var level3childA = new simpletest() { Name = "level3childA", Parent = level2childA };
var level3childB = new simpletest() { Name = "level3childB", Parent = level2childA };
var level3childC = new simpletest() { Name = "level3childC", Parent = level2childB };
var level3childD = new simpletest() { Name = "level3childD", Parent = level2childB };
var level3childE = new simpletest() { Name = "level3childE", Parent = level2childC };
var level3childF = new simpletest() { Name = "level3childF", Parent = level2childC };
var level3childG = new simpletest() { Name = "level3childG", Parent = level3childD };
var level3childH = new simpletest() { Name = "level3childH", Parent = level3childD };
level2childA.Children = new List<simpletest> { level3childA, level3childB };
level2childB.Children = new List<simpletest> { level3childC, level3childD };
level2childC.Children = new List<simpletest> { level3childE, level3childF };
level2childD.Children = new List<simpletest> { level3childG, level3childH };
level1childA.Children = new List<simpletest> { level2childA, level2childB };
level1childB.Children = new List<simpletest> { level2childC, level2childD };
因此,例如,使用节点 level3childE,我将如何从同一级别获取所有节点?节点名称可以是任何名称。