Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1838
  • Last Modified:

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

0
cnotley1982
Asked:
cnotley1982
1 Solution
 
Bob LearnedCommented:
I have read about LINQ and hierarchical data, and a quick review brings articles like these:

http://stackoverflow.com/questions/202912/hierarchical-data-in-linq-options-and-performance

.NET - LINQ AsHierarchy() extension method
http://www.scip.be/index.php?Page=ArticlesNET18

Hierarchical data in Linq - options and performance
I haven't tried anything like this, but it seems like a good thing to try...
0
 
cnotley1982Author Commented:
Thanks.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now