日期:2014-05-20 浏览次数:20747 次
public class recursionList { public recursionList(int id,int pid,string remark) { this.id = id; this.pid = pid; this.remark = remark; } public int id { get; set; } public int pid { get; set; } public string remark { get; set; } } public static List<recursionList> GetAllChirlds(List<recursionList> source,int id) { List<recursionList> result = new List<recursionList>(); recursionList parentM=source.FirstOrDefault(e => e.id == id); List<recursionList> chirldMs = source.Where(e => e.pid == parentM.id).ToList(); if (chirldMs.Count > 0) { result.AddRange(chirldMs); foreach (var item in chirldMs) { List<recursionList> subsubs = GetAllChirlds(source, item.id); if (subsubs.Count > 0) result.AddRange(subsubs); } } return result; } public static void Main(string[] args) { List<recursionList> templist = new List<recursionList>() { new recursionList(1,0,"A"), new recursionList(2,0,"A"), new recursionList(3,1,"B"), new recursionList(4,1,"B"), new recursionList(5,3,"C"), new recursionList(6,3,"C"), new recursionList(7,4,"D"), new recursionList(8,2,"D"), }; var allsubs = GetAllChirlds(templist, 1); }