首页新闻找找看学习计划

mvc 加载树形结构的问题

-1
悬赏园豆:10 [已解决问题] 解决于 2015-07-28 14:43

我现在在做一个树结构的功能,因根节点的值(在表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;
                        }
                    }
                }
            }
Aiolos丶M的主页 Aiolos丶M | 初学一级 | 园豆:162
提问于:2015-06-08 20:57
< >
分享
最佳答案
-1

把2个的数据都取出来

然后建立一个dict

对dict add (uid,a)

然后foreach b 通过关联key 对应上a

时间复杂度就n

收获园豆:5
小眼睛老鼠 | 老鸟四级 |园豆:2774 | 2015-06-16 13:32
其他回答(1)
-1

一张表就够了,表中加父节点ID标识列

收获园豆:5
wykCN | 园豆:216 (菜鸟二级) | 2015-06-09 09:08

 我也想一张表,问题是他们要两张表

支持(1) 反对(0) Aiolos丶M | 园豆:162 (初学一级) | 2015-06-09 21:06
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册