Note that there are some explanatory texts on larger screens.

plurals
  1. POLinq to build hierarchical list
    primarykey
    data
    text
    <p>I have a method that retrieves a set of records from a database.</p> <p>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.</p> <p>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.</p> <p>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.</p> <p>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.</p> <p>I have included the code that I am currently using, to hopefully better illustrate what I am trying to achieve.</p> <pre><code>var processes = new List&lt;Process&gt;(); var rootLevelProcesses = new List&lt;Process&gt;(); var allProcesses = new List&lt;Process&gt;(); foreach (Process process in e.Results) { process.ProcessChildren = new List&lt;Process&gt;(); if (process.ParentId &gt; 0 || (process.ParentId == 0 &amp;&amp; process.EntityId == 1)) { allProcesses.Add(process); } } var rootProcess = (from parent in e.Results where parent.EntityType == 1 &amp;&amp; parent.ContainerLevel &lt;= 1 select parent).FirstOrDefault(); processes.Add(rootProcess); var level2Processes = (from parent in allProcesses where parent.EntityType == 1 &amp;&amp; parent.ContainerLevel == 2 select parent).ToList(); foreach (Process process in level2Processes) { var level3Processes = (from parent in allProcesses where parent.EntityType == 1 &amp;&amp; parent.ContainerLevel == 3 select parent).ToList(); process.ProcessChildren = level3Processes; } processes[0].ProcessChildren = level2Processes; foreach (Process process in processes) { if (process.ProcessChildren != null &amp;&amp; process.ProcessChildren.Count &gt; 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; } } } } } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload