C# 递归程序 获取某个节点下的全部子节点

        /// <summary>
        /// 获取组织结构树
        /// </summary>
        /// <param name="list"></param>
        /// <param name="id"></param>
        /// <param name="treeNodes"></param>
        /// <returns></returns>
        static void GetOrganizeSelectTreeNodes(List<BaseOrganizeEntity> list, string id, ref List<SelectTreeNode> treeNodes)
        {
            if (list == null)
                return ;
            List<BaseOrganizeEntity> sublist;
            if (!string.IsNullOrWhiteSpace(id))
            {
                sublist = list.Where(t => t.ParentId == id).ToList();
            }
            else
            {
                sublist = list.Where(t => string.IsNullOrWhiteSpace(t.ParentId)).ToList();
            }
            if (!sublist.Any())
                return;
            foreach (var item in sublist)
            {
                treeNodes.Add(new SelectTreeNode() { id = item.Id, name = item.FullName, parentId = item.ParentId });
                GetOrganizeSelectTreeNodes(list, item.Id, ref treeNodes);
            }
        }

  

        /// <summary>
        /// 获取公司树
        /// </summary>
        /// <returns></returns>
        public ActionResult GetOrganizesTree(string id = null, string name = null)
        {
             // 使用程序递归 MySql用SQL语句递归比较暂时不支持,需要用存储过程处理
            List<SelectTreeNode> treeNodes = new List<SelectTreeNode>();
            GetOrganizeSelectTreeNodes(organizeManager.GetList<BaseOrganizeEntity>(), id, ref treeNodes);
            var result = new Hashtable { { "treeNodes", treeNodes } };
            return Json(result, JsonRequestBehavior.AllowGet);
        }

  

posted @ 2019-03-06 22:44  86727515  阅读(1320)  评论(0编辑  收藏  举报