Saxman
Saxman

Reputation: 5089

Group item into Parent -> Child -> Grand-child relationship

I have class as follow:

public class Item
    {
        public int ItemID { get; set; }
        public int GroupLevel { get; set; }
        public string ItemName { get; set; }
        public string ItemDesc { get; set; }
    }

This return an flatten IEnumerable result, but I need to group them into a Parent -> Child -> Grand-child relationship, based on GroupLevel and ItemID. Here's how the current data look like:

ItemID:     1
GroupLevel: 1
ItemName:   All Groups
ItemDesc:   Item 1 description
ParentID:   null

ItemID:     2
GroupLevel: 2
ItemName:   Boulder
ItemDesc:   Boulder description
ParentID:   1

ItemID:     3
GroupLevel: 2
ItemName:   Atlanta
ItemDesc:   Atlanta description
ParentID:   1

ItemID:     4
GroupLevel: 3
ItemName:   Boulder - Boulder south
ItemDesc:   Grand-child of Boulder (ID: 2)
ParentID:   2

ItemID:     5
GroupLevel: 3
ItemName:   Boulder - Boulder North
ItemDesc:   Another grand-child of Boulder (ID: 2)
ParentID:   2

ItemID:     6
GroupLevel: 3
ItemName:   Atlanta - West
ItemDesc:   Grand-child of Atlanta (ID: 3)
ParentID:   3

I would like to have the data structured like this:

ItemID:     1
GroupLevel: 1
ItemName:   All Groups
ItemDesc:   Item 1 description
ParentID:       null

    ItemID:     2
    GroupLevel: 2
    ItemName:   Boulder
    ItemDesc:   Boulder description
    ParentID:       1

        ItemID:     4
        GroupLevel: 3
        ItemName:   Boulder - Boulder south
        ItemDesc:   Grand-child of Boulder (ID: 2)
        ParentID:       2

        ItemID:     5
        GroupLevel: 3
        ItemName:   Boulder - Boulder North
        ItemDesc:   Another grand-child of Boulder (ID: 2)
        ParentID:       2


    ItemID:     3
    GroupLevel: 2
    ItemName:   Atlanta
    ItemDesc:   Atlanta description
    ParentID:       1

        ItemID:     6
        GroupLevel: 3
        ItemName:   Atlanta - West
        ItemDesc:   Grand-child of Atlanta (ID: 3)
        ParentID:       3

Any suggestion? Thank you.

Upvotes: 1

Views: 852

Answers (2)

shenhengbin
shenhengbin

Reputation: 4294

I think this is useful for you

linq-groupbymany-dynamically

You can read the sample , and create the Parent -> Child -> Grand-child relationship tree as you like .....

Upvotes: 0

Marking
Marking

Reputation: 784

Do you mean something like this:

    List<Item> itemlist;
    List<Item> newItemList;

    public void main()
    {
        //Find all root items (aka, no parents)
        foreach (Item item in itemlist)
        {
            if (item.Parent == null) getObjects(item.ID, 0);
        }
    }

    public void getObjects(Item me, int deep)
    {
        //Store node
        Console.WriteLine("this is me: " + me.ID);
        Console.WriteLine("I am this many levels deep: " + deep);
        newItemList.Insert(me);

        //Find my children
        foreach (Item item in itemlist)
        {
            if (item.Parent == me.ID) getObjects(item.ID, (deep + 1));
        }
    }

Upvotes: 1

Related Questions