c#.NET中各种递归

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 =&gt; zz.ParentId == id &amp;&amp; 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&lt;List&lt;ResourceDirectorysListDto&gt;&gt;<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&lt;ResourceDirectorysListDto&gt;<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&lt;ResourceDirectorysListDto&gt; 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&lt;ResourceDirectorysListDto&gt; childList = <span style="color: rgba(0, 0, 255, 1)">new</span> List&lt;ResourceDirectorysListDto&gt;<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 =&gt; 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>
复制代码
原文地址:https://www.cnblogs.com/sunny3158/p/14979223.html