65.9K
CodeProject is changing. Read more.
Home

A simple generic TreeList

starIconstarIconstarIconstarIconemptyStarIcon

4.00/5 (3 votes)

Mar 18, 2012

CPOL
viewsIcon

13532

downloadIcon

164

This is an alternative for "A simple generic TreeList".

Introduction 

This tip shows a way to structure data in a treelist, depending on 1 (or more) of an objects properties.

In this example I will show how to transform a list of persons into a hierarchical treelist, depending on their supervisor, and into another treelist, depending on their clients.

You can find more examples for both a Console Application and a Web Application inside the source code. 

Using the code  

Data: 

private static IEnumerable<Person> getPersons() {
    var eric = new Person { ID = 1, Name = "Eric", Supervisor = null, 
        ClientIDs = new int[] { 2 } };
    var stephanie = new Person { ID = 2, Name = "Stephanie", Supervisor = null, 
        ClientIDs = new int[] { 5 } };
    var caroline = new Person { ID = 3, Name = "Caroline", Supervisor = eric, 
        ClientIDs = new int[] { 4, 5 } };
    var ridge = new Person { ID = 4, Name = "Ridge", Supervisor = stephanie, 
        ClientIDs = new int[] { 5 } };
    var brooke = new Person { ID = 5, Name = "Brooke", Supervisor = eric, 
        ClientIDs = new int[] { 6, 7 } };
    var taylor = new Person { ID = 6, Name = "Taylor", Supervisor = ridge, 
        ClientIDs = new int[] { 8 } };
    var thorne = new Person { ID = 7, Name = "Thorne", Supervisor = brooke };
    var macy = new Person { ID = 8, Name = "Macy", Supervisor = ridge };
    return new List<Person> { eric, stephanie, caroline, ridge, brooke, taylor, thorne, macy };
}

Single parent (Supervisor):

var persons = getPersons();
var treeBySupervisor = persons.ToTreeList(
    p => p.ID, //Primary key
    p => p.Supervisor //Foreign key
);

Multiple parents (Clients):

var persons = getPersons();
var treeByClients = persons.ToTreeList(
    s => s.ID, //Primary key
    s => s.ClientIDs //Foreign keys
);

Extensions:

//TreeNode extensions
public static TreeNode<T> GetRoot<T>(this TreeNode<T> item)
public static IEnumerable<TreeNode<T>> GetAncestors<T>(this TreeNode<T> item)
public static IEnumerable<TreeNode<T>> GetChildren<T>(this TreeNode<T> item)
public static IEnumerable<TreeNode<T>> GetOffspring<T>(this TreeNode<T> item)
public static IEnumerable<TreeNode<T>> GetBrothers<T>(this TreeNode<T> item)
public static IEnumerable<TreeNode<T>> GetUncles<T>(this TreeNode<T> item)
public static IEnumerable<TreeNode<T>> GetNephews<T>(this TreeNode<T> item)

//TreeList extensions
public static IEnumerable<TreeNode<T>> GetSelf<T>(
       this IEnumerable<TreeNode<T>> collection, IEnumerable<T> selectedItems)
public static IEnumerable<TreeNode<T>> GetSelf<T>(this IEnumerable<TreeNode<T>> collection, T item)
public static IEnumerable<TreeNode<T>> GetParents<T>(this IEnumerable<TreeNode<T>> collection)
public static IEnumerable<TreeNode<T>> GetAncestors<T>(this IEnumerable<TreeNode<T>> collection)
public static IEnumerable<TreeNode<T>> GetRoots<T>(this IEnumerable<TreeNode<T>> collection)
public static IEnumerable<TreeNode<T>> GetChildren<T>(this IEnumerable<TreeNode<T>> collection)
public static IEnumerable<TreeNode<T>> GetOffspring<T>(this IEnumerable<TreeNode<T>> collection)
public static IEnumerable<TreeNode<T>> GetBrothers<T>(this IEnumerable<TreeNode<T>> collection)
public static IEnumerable<TreeNode<T>> GetUncles<T>(
       this IEnumerable<TreeNode<T>> collection)