树形结构数据获取

记录代码如下:
///

    /// 返回树形下拉框 菜单数据
    /// 
/// [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(); 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 TreeRecursion(List data, string parentId = "0", int hosid = 0) { List item = null; List newList = new List(); //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; }

你可能感兴趣的:(c#)