乐趣区

关于c#:树形结构数据获取

记录代码如下:
/// <summary>

    /// 返回树形下拉框 菜单数据
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    public ActionResult GetTreeList(int hosid=0,int pid=0)
    {var hoslist = yqBll.GetListall();
        var dptmodel = departmentBLL.GetModelList("F_State=1");
        dptmodel.Sort((x, y) => x.F_Sort ?? 0 - y.F_Sort ?? 0);// 排序
        var treeList = new List<TreeModel>();
        if (hoslist.Count > 0)
        {foreach (var hositem in hoslist)
            {TreeModel treeModel = new TreeModel();
                treeModel.id = hositem.T_Woid.ToString();
                treeModel.IconCls = "";
                treeModel.text = hositem.T_Woname;
                treeModel.parentid = "-1";
                treeModel.TypeId = 1;
                treeModel.children = TreeRecursion(dptmodel, "0", hositem.T_Woid);
                treeList.Add(treeModel);
                //var dptlist = dptmodel.Where(p => p.F_ParentId == 0 && p.T_Woid == hositem.T_Woid).ToList();}
        }
        //return Content(treeList.ToJson());
        return Success("获取菜单树胜利", treeList);
    }

    private List<TreeModel> TreeRecursion(List<Model.T_Sys_Department> data, string parentId = "0", int hosid = 0)
    {
        List<Model.T_Sys_Department> item = null;
        List<TreeModel> newList = new List<TreeModel>();
        //if (parentId != "0")
        //{if (hosid != 0)
        {item = data.FindAll(t => t.F_ParentId.ToString() == parentId && t.T_Woid == hosid);//data 倡议在调用此扩大办法前曾经排序过
        }
        else
        {item = data.FindAll(t => t.F_ParentId.ToString() == parentId);//data 倡议在调用此扩大办法前曾经排序过
        }
        if (item.Count > 0)
        {foreach (Model.T_Sys_Department entity in item)
            {TreeModel treeModel = new TreeModel();
                treeModel.id = entity.F_DeptId.ToString();
                treeModel.IconCls = "";
                treeModel.text = entity.F_DeptName;
                treeModel.parentid = entity.F_ParentId.ToString();
                treeModel.TypeId = 2;
                treeModel.children = TreeRecursion(item, entity.F_DeptId.ToString());
                newList.Add(treeModel);
            }
        }
        //}
        return newList;
    }
退出移动版