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; }
标签:递归,list,List,Linq,regions,GetRegion,query,id From: https://www.cnblogs.com/xiangshifu/p/16989109.html