https://www.cnblogs.com/wangyinlon/p/11679180.html
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)
{
</span><span style="color: rgba(0, 0, 255, 1)">var</span> menuRoot = <span style="color: rgba(0, 0, 255, 1)">await</span> _ResourceDirectorysRepository.GetAll().Where(zz => zz.ParentId == id && zz.IsDeleted == <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">).ToListAsync();
</span><span style="color: rgba(0, 0, 255, 1)">var</span> menuList = ObjectMapper.Map<List<ResourceDirectorysListDto>><span style="color: rgba(0, 0, 0, 1)">(menuRoot);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 为一级菜单设置子菜单,getChild是递归调用的</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> temp = <span style="color: rgba(0, 0, 255, 1)">new</span> List<ResourceDirectorysListDto><span style="color: rgba(0, 0, 0, 1)">();
</span><span style="color: rgba(0, 0, 255, 1)">foreach</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> item <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> menuList)
{
temp.Add(item);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> t =<span style="color: rgba(0, 0, 0, 1)"> GetChild2(item.Id);
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (t != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">)
{
item.IsParent </span>= <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
temp.AddRange(t);
}
</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">
{
item.IsParent </span>= <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
}
}
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> temp;
}
</span><span style="color: rgba(0, 0, 255, 1)">private</span> List<ResourceDirectorysListDto> GetChild2(<span style="color: rgba(0, 0, 255, 1)">long</span><span style="color: rgba(0, 0, 0, 1)"> id)
{
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">子菜单</span>
List<ResourceDirectorysListDto> childList = <span style="color: rgba(0, 0, 255, 1)">new</span> List<ResourceDirectorysListDto><span style="color: rgba(0, 0, 0, 1)">();
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 遍历所有节点,将父菜单id与传过来的id比较</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> list = _ResourceDirectorysRepository.GetAll().Where(x => x.ParentId ==<span style="color: rgba(0, 0, 0, 1)"> id).ToList();
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">list.Any())
{
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
}
</span><span style="color: rgba(0, 0, 255, 1)">foreach</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> item <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> list)
{
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (item.ParentId ==<span style="color: rgba(0, 0, 0, 1)"> id)
{
</span><span style="color: rgba(0, 0, 255, 1)">var</span> z = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ResourceDirectorysListDto
{
Id </span>=<span style="color: rgba(0, 0, 0, 1)"> item.Id,
ParentId </span>=<span style="color: rgba(0, 0, 0, 1)"> item.ParentId,
Name </span>=<span style="color: rgba(0, 0, 0, 1)"> item.Name,
};
childList.Add(z);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> t =<span style="color: rgba(0, 0, 0, 1)"> GetChild2(item.Id);
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (t != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">)
{
z.IsParent </span>= <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
childList.AddRange(t);
}
</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">
{
z.IsParent </span>= <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
}
}
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 递归退出条件</span>
<span style="color: rgba(0, 0, 255, 1)">if</span> (childList.Count == <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">)
{
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
}
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> childList;
}</span></pre>
标签:
c#
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)