.net directory structure

 Conventional number of directory entity framework

. 1      public  class zTree
 2      {
 . 3          ///  <Summary> 
. 4          /// tree control node entities
 . 5          ///  </ Summary> 
. 6  
. 7              ///  <Summary> 
. 8              /// identifier
 . 9              ///  </ Summary> 
10              public  String ID { GET ; SET ;}
 . 11  
12 is              ///  <Summary> 
13 is              /// Title
 14              ///  </ Summary> 
15              public  String name { GET ;set; }
16             public bool open { get; set; }
17             public string type { get; set; }
18             /// <summary>
19             /// 子节点
20             /// </summary>
21             public List<Ztree> children = new List<Ztree>();
22     }

1 when the data is different each class by virtue of the method used is not the same as when I was here two non'll show the same data registration

 

1.0 When all data structures have different kind of time we can use the cycle way to splice out slowly

 1   List<Ztree> createZtreeNode(List<areas> areas,List<instrument> instruments,List<sensor> sensors)
 2         {
 3             List<Ztree> Ztreeone = new List<Ztree>();
 4             Ztree Ztreeonen = new Ztree();
 5             Ztreeonen.id = "0";
 6             Ztreeonen.name = "全部";
 7             List<Ztree> Ztrees = new List<Ztree>();
 8             foreach (var a in areas)
 9             {
10                 Ztree Ztree = new Ztree();
11                 Ztree.id = a.id.ToString();
12                 Ztree.name = a.Name;
13                 Ztreeonen.open = true;
14                 Ztrees.Add(Ztree);
15                 Ztreeonen.children.Add(Ztree);
16             }
17          
18             foreach (var t in Ztrees)
19             {
20                 var results = instruments.Where(x=>x.AreaId.ToString()==t.id).ToList();
21                 foreach (var res in results)
22                 {
23                     Ztree Ztree = new Ztree();
24                     Ztree.id = t.id+","+ res.Id;//判断级数并绑定Id
25                     Ztree.name = res.Number;
26                     var sens=sensors.Where(x => x.InstrumentId == res.Id).ToList();
27                     foreach (var s in sens)
28                      {
 29                          zTree TR = new new zTree ();
 30                          tr.id = Ztree.id + " , " + s.Id; // Serieses and bind Id 
31 is                          tr.name = s.Number;
 32                          
33 is                          Ztree.children .add (TR);
 34 is                      }
 35                      Ztreeonen.open = to true ;
 36                      t.children.Add (zTree);
 37 [                  }
 38 is              }
 39  
40             Ztreeone.Add(Ztreeonen);
41             var abb = JsonConvert.SerializeObject(Ztreeone);
42             return Ztreeone;
43         }

 

2.0 The method of using the data it is gradually inserted recursive traversal of the binary tree form a bit like

 

 1    List<Tree> CreateZtreeNode(int Pid, List<Department> createZtreeNode)
 2         {
 3             List<Tree> trees = new List<Tree>();
 4             var childs = createZtreeNode.Where(d => d.SuperiorDepartmentsId == Pid && d.Deleted!=true);
 5             foreach (var Depar in childs)
 6             {
 7                 var tree = new Tree();
 8                 var newrelationship = new _relationship();
 9                 tree.id = Depar.DepartmentId;
10                 tree.name = Depar.DepartmentName + "<span style='display:none;'>*" + Depar.DepartmentId + "*</span>";//添加name,在后面加上标识
11                 try
12                 {
13                     tree.title = db.Users.Where(x => x.Id == Depar.PartOwner).FirstOrDefault().UserName;
14                 }
15                 catch (Exception)
16                 {
17                     tree.title = "";
18                 }
19                 newrelationship.children_num = createZtreeNode.Where(x => x.SuperiorDepartmentsId == Depar.DepartmentId && x.Deleted != true).Count();//下属部门
20                 newrelationship.parent_num = Depar.SuperiorDepartmentsId;
21                 newrelationship.sibling_num = createZtreeNode.Where(x => x.SuperiorDepartmentsId == Depar.SuperiorDepartmentsId && x.Deleted != true).Count() - 1;
22                 tree.relationship.Add(newrelationship);
23                 tree.children = CreateZtreeNode(tree.id, createZtreeNode);
24                 if (tree.relationship.Count == 0)
25                 {
26                     tree.relationship = null;
27                 }
28                 trees.Add(tree);
29             }
30             return trees;
31         }

Recursive and 2.1 above operation is similar to the file slightly

 1    private static List<DirectoryInfo> GetChild(List<DirectoryInfo> directoryList,DirectoryInfo  directoryCurrent)
 2         {
 3             var childArray = directoryCurrent.GetDirectories();
 4             if (childArray != null && childArray.Length > 0)
 5             directoryList.AddRange(childArray);
 6             foreach (var child in childArray)
 7             {
 8                 GetChild(directoryList, child);
 9             }
10             return directoryList;
11         }

General idea is shown below (constant-first traversal subset know can not be found so far)

 

Guess you like

Origin www.cnblogs.com/YZM97/p/11763756.html