Click here to Skip to main content
15,891,136 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

A simple generic TreeList

Rate me:
Please Sign up or sign in to vote.
4.00/5 (3 votes)
18 Mar 2012CPOL 13.3K   162   8   1
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: 

C#
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):

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

Multiple parents (Clients):

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

Extensions:

C#
//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)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Belgium Belgium
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionNeeds some work Pin
Richard MacCutchan16-Mar-12 8:10
mveRichard MacCutchan16-Mar-12 8:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.