递归 无穷找子类的递归
///id要找的父类的id ///把找到的子类的id存放到idList中 private void GetAllSonPlateId(Guid id, ref List<Guid> idList) { try { List<Guid> gets = _videoR.GetSonId(id); if (gets.Count > 0) { foreach (Guid item in gets) { idList.Add(id); GetAllSonPlateId(item, ref idList); } } else { idList.Add(id); } } catch (Exception) { } }
/// <summary> /// 获得一个栏目的子栏目的idList /// </summary> /// <returns></returns> public List<Guid> GetSonId(Guid id) { List<Guid> idList = new List<Guid>(); try { var list = from plate in _db.Plate where plate.ParentID == id select plate.ID; idList.AddRange(list.ToList<Guid>()); return idList; } catch (Exception) { return idList; } }
第二个代码
/// <summary> /// 词条的列表 有百科类型 /// </summary> public class WordList { public string BaikeType { get; set; } public Guid BaikeTypeID { get; set; } public List<Word> List { get; set; } } /// <summary> /// 词条列表 /// </summary> /// <returns></returns> [HttpPost] public ActionResult citiaolist(Guid? id) { if (id.HasValue) { List<BaiKeType> baikelist = new List<BaiKeType>(); List<WordList> list = new List<WordList>(); List<WordList> self = _baikeR.GetOneCitiaoList(id.Value); if (self != null) { list = list.Concat(self).ToList(); } GetBaikeTypeSon(id.Value, ref baikelist); foreach (BaiKeType item in baikelist) { List<WordList> temp = _baikeR.GetOneCitiaoList(item.ID); if (temp != null) { list = list.Concat(temp).ToList(); } } ViewData["list"] = list; } else { ViewData["list"] = _baikeR.GetCitiaoList(); } return View(); } //找到这个类别的所有子类别,返回到list中 private void GetBaikeTypeSon(Guid id, ref List<BaiKeType> list) { try { List<BaiKeType> baikelist = _baikeR.GetBaikeTypeListByParentID(id); if (baikelist != null && baikelist.Count > 0) { list = list.Concat(baikelist).ToList(); foreach (BaiKeType item in baikelist) { GetBaikeTypeSon(item.ID, ref list); } } } catch { } }