Linq递归

        public IEnumerable<NavigationMagnetics> GetRegion(List<NavigationMagnetics> list, int id)
        {
            IEnumerable<NavigationMagnetics> query = from region in list
                                        where region.Id == id
                                        select region;
            return query.ToList().Concat(query.ToList().SelectMany(r => GetRegion(list, r.Supid.ToInt32())));
        }
        // 生成最终结果
        public List<NavigationMagnetics> GetResult(List<NavigationMagnetics> list, int id)
        {
            List<NavigationMagnetics> regions = new List<NavigationMagnetics>();
            regions.AddRange(list.Where(r => r.Id == id));
            regions.AddRange(GetRegion(list, regions.Where(x=>x.Id== id).FirstOrDefault().Supid.ToInt32()));
            return regions;
        }

  

posted @ 2022-12-17 16:13  仲夏不凉爽  阅读(55)  评论(0编辑  收藏  举报