获取整颗树的另类写法。非递归
//获取所有的菜单
List<T_Menu> menu = bll.getMenuByUsesrID("8189a7c1-6f15-4744-b6c4-d3ee5b5b994d");
//创建EasyTree控件model List<EasyTreeModel> list = new List<EasyTreeModel>();
//把所有菜单添加到list集合中 menu.ForEach(o => { EasyTreeModel model = new EasyTreeModel(); model.id = o.C_ID; model.text = o.C_Name; model.pid = (int)o.C_Parent_ID; model.url = o.C_Url; list.Add(model); }); //遍历list,给所有元素添加子级。
//因下面方法操作对象是引用类型,所以最终第一级对象下会有所有的子级 list.ForEach(o => { o.children = list.Where(l => l.pid == o.id).ToList(); }); //所以最后只取第一级就是整颗树 return list.Where(o=> o.pid==0)._ToJsonStr();