标签:ati return class number new 思路 title create img
常规目录数的实体架构
1 public class Ztree 2 { 3 /// <summary> 4 /// 树控件节点实体 5 /// </summary> 6 7 /// <summary> 8 /// 标识 9 /// </summary> 10 public string id { get; set; } 11 12 /// <summary> 13 /// 名称 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当数据每一条都是不同的类的是时候所用的凭借方法不一样我在这里会展示两种不一样的数据拼接
1.0当所有的数据结构都有不同的类的时候我们就可以使用循环的方式将其慢慢的拼接出来
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 Ztree(); 30 tr.id = Ztree.id+","+s.Id;//判断级数并绑定Id 31 tr.name = s.Number; 32 33 Ztree.children.Add(tr); 34 } 35 Ztreeonen.open = true; 36 t.children.Add(Ztree); 37 } 38 } 39 40 Ztreeone.Add(Ztreeonen); 41 var abb = JsonConvert.SerializeObject(Ztreeone); 42 return Ztreeone; 43 }
2.0使用用插入递归的方法使其数据逐渐遍历,期形式有点像二叉树
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 }
2.1文件的递归操作 和上面的操作有些许相似
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 }
大致的思路图如下(不断的优先遍历子集知道找不到为止)
标签:ati return class number new 思路 title create img
原文地址:https://www.cnblogs.com/YZM97/p/11763756.html