using System.Collections.Generic;
using System.Collections;
public int Id { get; set; }
public int ParentId { get; set; }
public string Text { get; set; }
public static class ExtensionMethods
public static IEnumerable<FlatData> GetDescendents(this IEnumerable<FlatData> This, int rootId)
var rootItem = This.Single( x => x.Id == rootId );
var queue = new Queue<FlatData>( new [] { rootItem } );
var item = queue.Dequeue();
foreach (var child in This.Where( x => x.ParentId == item.Id ))
public static void Main()
FlatData[] elements = new FlatData[]
new FlatData {Id = 3, ParentId = 1, Text = "A"},
new FlatData {Id = 4, ParentId = 1, Text = "D"},
new FlatData {Id = 5, ParentId = 2, Text = "E"},
new FlatData {Id = 7, ParentId = 2, Text = "G"},
new FlatData {Id = 8, ParentId = 4, Text = "H"},
new FlatData {Id = 9, ParentId = 8, Text = "H"},
new FlatData {Id = 10, ParentId = 8, Text = "I"},
new FlatData {Id = 11, Text = "I"},
var filtered = elements.GetDescendents(4).ToList();
foreach (var f in filtered)
Console.WriteLine("{0} {1} {2}", f.Id, f.ParentId, f.Text);