人本善良

导航

.net 递归 父子表结构 生成树

 private static string RecursionMenuTree(List<Menu> menus, long parentID = 0, StringBuilder sb = null)
        {
            if (sb == null)
            {
                sb = new StringBuilder();
            }

            var parentMenus = menus.Where(x => x.ParentID == parentID).ToList();

            parentMenus.ForEach(x =>
             {
                 sb.Append("{");
                 sb.AppendFormat("\"ID\":\"{0}\",", x.ID.ToString());
                 sb.AppendFormat("\"Path\":\"{0}\",", x.Path);
                 sb.AppendFormat("\"MenuName\":\"{0}\",", x.MenuName);
                 sb.AppendFormat("\"OrderNum\":\"{0}\",", x.OrderNum.ToString());
                 sb.AppendFormat("\"Type\":\"{0}\"", x.Type.ToString());
                 var children = menus.Where(c => c.ParentID == x.ID);
                 if (children?.Count() > 0)
                 {
                     sb.Append("\"children\":[");
                     RecursionMenuTree(menus, x.ID, sb);
                     sb.Append("]");
                 }
                 sb.Append("},");
             });
            return sb.ToString();
        }


//获取子部门一直到最下级部门int型不牵扯部门人员
private void getmychildrendepartment1(int Department_id, dynamic data, ref List<int> list)
{
for (int i = 0; i < data.Count; i++)
{
if (data[i].ParentNo == Department_id)
{
list.Add(data[i].ID);
getmychildrendepartment1(data[i].ID, data, ref list);
}
}
}
//获取父级部门一直到最上级部门
private void getmyparentdepartment(string Department_id, dynamic data, ref List<string> list)
{
for (int i = 0; i < data.Count; i++)
{
if (Convert.ToString(data[i].ID) == Department_id)
{
list.Add("'" + data[i].ParentNo + "'");
getmyparentdepartment(data[i].ParentNo, data, ref list);
}
}
}

 

 

posted on 2021-05-07 15:10  简简单单2018  阅读(199)  评论(0编辑  收藏  举报