public void RecursiveTest() { var testTree = new TestTree("Parent"); var child1 = new TestTree("Child_1"); child1.Children.AddRange(new[] { new TestTree("Child_1-a"), new TestTree("Child_1-b"), }); var child2 = new TestTree("Child_2"); child2.Children.AddRange(new[] { new TestTree("Child_2-a"), new TestTree("Child_2-b"), new TestTree("Child_2-c"), }); var child3 = new TestTree("Child_3"); child3.Children.AddRange(new[] { new TestTree("Child_3-a"), new TestTree("Child_3-b"), }); var child3c = new TestTree("Child_3-c"); child3c.Children.AddRange(new[] { new TestTree("Child_3-c-1"), new TestTree("Child_3-c-2"), new TestTree("Child_3-c-3"), }); child3.Children.Add(child3c); child3.Children.Add(new TestTree("Child_3-d")); testTree.Children.Add(child1); testTree.Children.Add(child2); testTree.Children.Add(child3); var actual = testTree .Recursive(each => each.Children) .Select(tree => tree.Value); var expected = new[] { "Child_1", "Child_2", "Child_3", "Child_1-a", "Child_1-b", "Child_2-a", "Child_2-b", "Child_2-c", "Child_3-a", "Child_3-b", "Child_3-c", "Child_3-d", "Child_3-c-1", "Child_3-c-2", "Child_3-c-3", }; Assert.IsTrue(actual.SequenceEqual(expected)); }