Link to home
Start Free TrialLog in
Avatar of cnotley1982
cnotley1982

asked on

Linq To Build Hierarchical List

I have a method that retrieves a set of records from a database.

I map this data to a business object - let's call the business object ProcessModel.  One of the properties in ProcessModel is a list, called ProcessChildren.  The property is List<ProcessModel>.

So, the data is linked by various fields/properties.  There is one object at the top of the hierarchical list, then that object has multiple objects in it's ProcessChildren property, those objects have multiple objects in their ProcessChildren properties etc.

Anyway, I wrote quite a bit of code to iterate through the dataset returned, and build the hierarchical list, which I then bind to a TreeView in Silverlight.

I would be very appreciative if someone could provide me with a much cleaner, simpler way to do this, either using linq, or using linq or some other approach via means of an extension method.

I have included the code that I am currently using, to hopefully better illustrate what I am trying to achieve.
var processes = new List<Process>();

            var rootLevelProcesses = new List<Process>();

            var allProcesses = new List<Process>();

            foreach (Process process in e.Results)
            {
                process.ProcessChildren = new List<Process>();

                if (process.ParentId > 0 || (process.ParentId == 0 && process.EntityId == 1))
                {
                    allProcesses.Add(process);
                }
            }

            var rootProcess =
                        (from parent in e.Results
                         where parent.EntityType == 1 && parent.ContainerLevel <= 1
                         select parent).FirstOrDefault();

            processes.Add(rootProcess);

            var level2Processes = (from parent in allProcesses
                                   where parent.EntityType == 1 && parent.ContainerLevel == 2
                                   select parent).ToList();

            foreach (Process process in level2Processes)
            {
                var level3Processes = (from parent in allProcesses
                                       where parent.EntityType == 1 && parent.ContainerLevel == 3
                                       select parent).ToList();

                process.ProcessChildren = level3Processes;
            }

            processes[0].ProcessChildren = level2Processes;

            foreach (Process process in processes)
            {
                if (process.ProcessChildren != null && process.ProcessChildren.Count > 0)
                {
                    foreach (Process level1 in process.ProcessChildren)
                    {
                        if (level1.EntityType == 1)
                        {
                            var children =
                            (from child in allProcesses
                             where child.ParentId == level1.EntityId
                             select child).ToList();

                            level1.ProcessChildren = children;

                            foreach (Process level2 in level1.ProcessChildren)
                            {
                                if (level2.EntityType == 1)
                                {
                                    children =
                                (from child in allProcesses
                                 where child.ParentId == level2.EntityId
                                 select child).ToList();

                                    level2.ProcessChildren = children;

                                    foreach (Process level3 in level2.ProcessChildren)
                                    {
                                        if (level3.EntityType == 1)
                                        {
                                            children =
                                        (from child in allProcesses
                                         where child.ParentId == level3.EntityId
                                         select child).ToList();

                                            level3.ProcessChildren = children;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cnotley1982
cnotley1982

ASKER

Thanks.