c#.NET中各种递归
最近工作中涉及到菜单,各种递归.总结了2种.
按父级子级生成树
public async Task<List<ResourceDirectorysListTreeDto>> GetTreeByParentId(long id, int getChildCount = 0) { var menuAll = _ResourceDirectorysRepository.GetAll().Where(zz => zz.IsDeleted == false); var menuRoot = await _ResourceDirectorysRepository.GetAll().Where(zz => zz.ParentId == id && zz.IsDeleted == false).ToListAsync(); var menuList = ObjectMapper.Map<List<ResourceDirectorysListTreeDto>>(menuRoot); // 为一级菜单设置子菜单,getChild是递归调用的 foreach (var item in menuList) { item.Child = GetChild(item.Id, menuAll.ToList(), 1, getChildCount); } return menuList; } private List<ResourceDirectorysListTreeDto> GetChild(long id, List<EntityDesign.Resourses.ResourceDirectorys> menuAll, int excouteCout, int getChildCount) { if (getChildCount > 0 && excouteCout > getChildCount) { return null; } //子菜单 List<ResourceDirectorysListTreeDto> childList = new List<ResourceDirectorysListTreeDto>(); // 遍历所有节点,将父菜单id与传过来的id比较 var list = menuAll.Where(x => x.ParentId == id).ToList(); if (!list.Any()) { return null; } foreach (var item in list) { if (item.ParentId == id) { childList.Add(new ResourceDirectorysListTreeDto { Id = item.Id, ParentId = item.ParentId, Name = item.Name, IndexLevel = item.IndexLevel }); } } // 把子菜单的子菜单再循环一遍 foreach (var item in childList) { item.Child = GetChild(item.Id, menuAll, excouteCout + 1, getChildCount); } // 递归退出条件 if (childList.Count == 0) { return null; } return childList; }
按父级子级生成List
public async Task<List<ResourceDirectorysListDto>> GetAllListByParentId(long id) { var menuRoot = await _ResourceDirectorysRepository.GetAll().Where(zz => zz.ParentId == id && zz.IsDeleted == false).ToListAsync(); var menuList = ObjectMapper.Map<List<ResourceDirectorysListDto>>(menuRoot); // 为一级菜单设置子菜单,getChild是递归调用的 var temp = new List<ResourceDirectorysListDto>(); foreach (var item in menuList) { temp.Add(item); var t = GetChild2(item.Id); if (t != null) { item.IsParent = true; temp.AddRange(t); } else { item.IsParent = false; } } return temp; } private List<ResourceDirectorysListDto> GetChild2(long id) { //子菜单 List<ResourceDirectorysListDto> childList = new List<ResourceDirectorysListDto>(); // 遍历所有节点,将父菜单id与传过来的id比较 var list = _ResourceDirectorysRepository.GetAll().Where(x => x.ParentId == id).ToList(); if (!list.Any()) { return null; } foreach (var item in list) { if (item.ParentId == id) { var z = new ResourceDirectorysListDto { Id = item.Id, ParentId = item.ParentId, Name = item.Name, }; childList.Add(z); var t = GetChild2(item.Id); if (t != null) { z.IsParent = true; childList.AddRange(t); } else { z.IsParent = false; } } } // 递归退出条件 if (childList.Count == 0) { return null; } return childList; }
作者:银龙
-------------------------------------------
个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!
万水千山总是情,打赏一分行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主,哈哈哈(っ•̀ω•́)っ✎⁾⁾!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
2017-10-15 WinForm 中 comboBox控件之数据绑定