我现在在做一个树结构的功能,因根节点的值(在表A中)与子节点的值(在表B中)不在同一张表中,现在我是通过先查询表A找到根节点,然后通过表B找到子节点,如果子节点后面还有子节点,类似上面,通过当前子节点查询表B,然后找到子节点,这样写的代码太烂了,有没有更好的方法实现这个功能
string[] result = id.Split(','); List<TreeItemModel> treeItem = new List<TreeItemModel>(); if (result[1] == "fristload") { OrganizeBll orgBll = new OrganizeBll(); SecurityOrganizationBll sorgBll = new SecurityOrganizationBll(); List<Organize> orgList = orgBll.GetModelList("ParentID='" + result[0] + "' and name like '%大队%' order by Sort"); //查询四级单位集合 if (orgList != null && orgList.Count > 0) { for (int i = 0; i < orgList.Count; i++) { Organize orgModel = orgList[i]; TreeItemModel treeItemModel = new TreeItemModel(); treeItemModel.text = orgModel.Name; treeItemModel.id = orgModel.ID.ToString(); treeItem.Add(treeItemModel); List<SecurityOrganization> childList = sorgBll.GetModelList("parent_department_identifier='" + orgModel.ID + "'"); if (childList.Count > 0) { treeItemModel.type = "folder"; TreeChildModel treeChildModel = new TreeChildModel(); treeItemModel.additionalParameters = treeChildModel; treeItemModel.additionalParameters.Id = orgModel.ID.ToString(); } else { treeItemModel.type = "item"; TreeChildModel treeChildModel = new TreeChildModel(); treeChildModel.Id = orgModel.ID.ToString(); treeChildModel.IsSelect = true; treeItemModel.additionalParameters = treeChildModel; } } } } else { SecurityOrganizationBll soBll = new SecurityOrganizationBll(); List<SecurityOrganization> soList = soBll.GetModelList("parent_department_identifier='"+result[0]+"'"); if (soList != null && soList.Count > 0) { for (int i = 0; i < soList.Count; i++) { SecurityOrganization model = soList[i]; TreeItemModel treeItemModel = new TreeItemModel(); treeItemModel.text = model.department_name; treeItemModel.id = model.department_identifier.ToString(); treeItem.Add(treeItemModel); List<SecurityOrganization> childList = soBll.GetModelList("parent_department_identifier='" + model.department_identifier + "'"); if (childList.Count > 0) { treeItemModel.type = "folder"; TreeChildModel treeChildModel = new TreeChildModel(); treeItemModel.additionalParameters = treeChildModel; treeItemModel.additionalParameters.Id = model.department_identifier.ToString(); } else { treeItemModel.type = "item"; TreeChildModel treeChildModel = new TreeChildModel(); treeChildModel.Id = model.department_identifier.ToString(); treeChildModel.IsSelect = true; treeItemModel.additionalParameters = treeChildModel; } } } }
把2个的数据都取出来
然后建立一个dict
对dict add (uid,a)
然后foreach b 通过关联key 对应上a
时间复杂度就n
一张表就够了,表中加父节点ID标识列
我也想一张表,问题是他们要两张表