我怎么能做递归linq函数?

时间:2016-09-08 20:04:05

标签: c# linq recursion drop-down-menu submenu

我想制作一个菜单,这是我的代码。我需要递归函数。

        var newTopMenu = new TopMenuVm
        {
            TopMenus = (from amp in allMasterPages
                        orderby amp.DisplayOrder
                        select new TopMenuVm
                        {
                            DisplayOrder = amp.DisplayOrder,
                            Url = string.IsNullOrEmpty(amp.Url) ? GenerateUrl(amp.Page1.OrderBy(x => x.DisplayOrder).FirstOrDefault()) : amp.Url,
                            PageId = amp.PageId,
                            PageTitle = amp.PageTitle,
                            TopMenus = (from submenu in amp.Page1
                                        orderby amp.DisplayOrder
                                        select new TopMenuVm
                                        {
                                            DisplayOrder = submenu.DisplayOrder,
                                            PageTitle = submenu.PageTitle,
                                            PageId = submenu.PageId,
                                            Url = string.IsNullOrEmpty(submenu.Url) ? GenerateUrl(submenu) : submenu.Url,
                                        }).ToList()//Here is go on infinite...,

                        }).ToList()
        };

我如何用linq做到这一点,请帮助...

1 个答案:

答案 0 :(得分:0)

这是你想要的东西吗?

while

当然,它不一定是Func<>,它可能只是一个返回List< TopMenuVM>的常规方法。并获取List< MyPage> (或任何allMasterPages)。