public void LoadDataStructure() { this.topNode = staticLoadTree(); //get the top level nodes //then add in all the children using (CSET_Context db = new CSET_Context()) { var result = from a in db.MATURITY_QUESTIONS join b in db.MATURITY_GROUPINGS on a.Grouping_Id equals b.Grouping_Id where a.Maturity_Model_Id == 3 select new { a, b }; Dictionary <int, string> questionIDtoTitle = new Dictionary <int, string>(); foreach (var q in result.ToList()) { questionIDtoTitle.Add(q.a.Mat_Question_Id, q.a.Question_Title); ScoringNode t = midNodes[q.b.Title_Id]; if (q.a.Parent_Question_Id == null) { LeafNode l = processLeafNode(q.a.Mat_Question_Id, q.a.Question_Title, t); t.Children.Add(l); } else { //remove the parent question from leaves dictionary //remove the parent question from it's parent's child collection //add it to the children of t as new mid node //then add all the children to this new mid node //note that at this poing Parent_Question_Id should never be null ScoringNode outNode; string parentTitle = questionIDtoTitle[q.a.Parent_Question_Id ?? 0]; if (midNodes.TryGetValue(parentTitle, out outNode)) { LeafNode l = processLeafNode(q.a.Mat_Question_Id, q.a.Question_Title, outNode); outNode.Children.Add(l); } else { LeafNode parentLeaf = leafNodes[q.a.Parent_Question_Id ?? 0]; parentLeaf.Parent.Children.Remove(parentLeaf); leafNodes.Remove(q.a.Parent_Question_Id ?? 0); MidlevelScoreNode node = new MidlevelScoreNode() { Title_Id = parentTitle, Grouping_Id = q.a.Grouping_Id ?? 0, Description = "Parent of " + q.a.Question_Title }; midNodes.Add(parentTitle, node); LeafNode l = processLeafNode(q.a.Mat_Question_Id, q.a.Question_Title, node); node.Children.Add(l); t.Children.Add(node); } } } } }
private void addChildrenToList(ScoringNode node, Dictionary <String, ScoringNode> nodesList) { foreach (ScoringNode n in node.Children) { nodesList.Add(n.Title_Id, n); if (n.Children.Count > 0) { addChildrenToList(n, nodesList); } } }
private LeafNode processLeafNode(int questionid, string title_id, ScoringNode t) { LeafNode l = new LeafNode() { Mat_Question_Id = questionid, Title_Id = title_id, Parent = t }; leafNodes.Add(questionid, l); return(l); }