Recursive List Flattening

后端 未结 13 1270
既然无缘
既然无缘 2020-11-27 04:25

I could probably write this myself, but the specific way I\'m trying to accomplish it is throwing me off. I\'m trying to write a generic extension method similar to the oth

相关标签:
13条回答
  • 2020-11-27 05:32
    class PageViewModel { 
        public IEnumerable<PageViewModel> ChildrenPages { get; set; } 
    }
    
    Func<IEnumerable<PageViewModel>, IEnumerable<PageViewModel>> concatAll = null;
    concatAll = list => list.SelectMany(l => l.ChildrenPages.Any() ? 
        concatAll(l.ChildrenPages).Union(new[] { l }) : new[] { l });
    
    var allPages = concatAll(source).ToArray();
    
    0 讨论(0)
提交回复
热议问题