C# .NetCore简单实现无限递归的功能
1:在实际开发中,我们会经常使用到无限递归的情况,如菜单,父子级等的情况
2:Code
1 using System; 2 using System.Collections.Generic; 3 using ConsoleApp1.Models; 4 using System.Linq; 5 using Newtonsoft.Json; 6 namespace ConsoleApp1 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 var allData = GetListData(); 13 var parentNode = allData.Where(p => p.PParentId == 0).ToList(); 14 List<PersonModel> tree = new List<PersonModel>(); 15 foreach (var item in parentNode) 16 { 17 PersonModel p1 = new PersonModel { Children = new List<PersonModel> { } }; 18 int _id = item.ID; 19 p1.ID = _id; 20 p1.Pname = item.Pname; 21 p1.Age = item.Age; 22 p1.PParentId = item.PParentId; 23 GetNewNodes(allData, p1); 24 tree.Add(p1); 25 } 26 string jsonTree = JsonConvert.SerializeObject(tree, Formatting.Indented); 27 Console.WriteLine(jsonTree); 28 Console.ReadKey(); 29 } 30 31 static void GetNewNodes(List<PersonModel> all, PersonModel curItem) 32 { 33 var subItems = all.Where(c => c.PParentId == curItem.ID).ToList(); 34 curItem.Children = new List<PersonModel>(); 35 curItem.Children.AddRange(subItems); 36 foreach (var subItem in subItems) 37 { 38 GetNewNodes(all, subItem); 39 } 40 } 41 static List<PersonModel> GetListData() 42 { 43 return new List<PersonModel> { 44 new PersonModel{ID=1001,Age=68,Pname="QQ1",PParentId=0 }, 45 new PersonModel{ID=1002,Age=78,Pname="ww",PParentId=0 }, 46 new PersonModel{ID=1003,Age=67,Pname="dd",PParentId=0 }, 47 new PersonModel{ID=1004,Age=88,Pname="ff",PParentId=0 }, 48 49 new PersonModel{ID=1005,Age=18,Pname="gg",PParentId=1001}, 50 new PersonModel{ID=1006,Age=16,Pname="cc",PParentId=1003}, 51 new PersonModel{ID=1007,Age=17,Pname="aa",PParentId=1006}, 52 new PersonModel{ID=1008,Age=19,Pname="tt",PParentId=1007}, 53 new PersonModel{ID=1009,Age=20,Pname="ii",PParentId=1004}, 54 new PersonModel{ID=1010,Age=22,Pname="ggf",PParentId=1003} 55 }; 56 } 57 } 58 }
3:测试ok截图
List<GetMenuInfos> allListDta = GetListMenuData(); //拿到所有父及节点 var parentMenulist = allListDta.Where(c => c.pid == 0).ToList(); List<GetMenuInfos> tree = new List<GetMenuInfos>();//类型一致 foreach (var item in parentMenulist) { var menuInfo = new GetMenuInfos() { mid = item.mid, name = item.name, pid = item.pid, children = new List<GetMenuInfos>() }; LoopLoadMenuData(allListDta, menuInfo); tree.Add(menuInfo); } string jsonStr = JsonConvert.SerializeObject(tree, Formatting.Indented); Console.WriteLine(jsonStr); Console.ReadKey();
Loop Code
private static void LoopLoadMenuData(List<GetMenuInfos> allListDta, GetMenuInfos menuInfo) { //子级下面的所有数据都在递归里面处理 var thischilds = allListDta.Where(c => c.pid == menuInfo.mid).ToList(); menuInfo.children = new List<GetMenuInfos>(); menuInfo.children.AddRange(thischilds); foreach (var item in thischilds) { LoopLoadMenuData(allListDta, item); } }
实体以及实体的数据格式
public static List<GetMenuInfos> GetListMenuData() { return new List<GetMenuInfos>() { new GetMenuInfos { mid=1,name="qq",pid=0}, new GetMenuInfos { mid=2,name="ww",pid=1}, new GetMenuInfos { mid=3,name="ee",pid=2}, new GetMenuInfos { mid=4,name="rr",pid=1}, new GetMenuInfos { mid=5,name="tt",pid=0}, new GetMenuInfos { mid=6,name="yy",pid=0}, new GetMenuInfos { mid=7,name="uu",pid=6}, new GetMenuInfos { mid=8,name="ii",pid=6}, new GetMenuInfos { mid=9,name="oo",pid=5}, new GetMenuInfos { mid=10,name="pp",pid=2}, new GetMenuInfos { mid=11,name="aa",pid=3}, new GetMenuInfos { mid=12,name="ss",pid=14}, new GetMenuInfos { mid=13,name="dd",pid=0}, new GetMenuInfos { mid=14,name="ff",pid=4}, }; } /* 定义的实体模型 public class GetMenuInfos { public int mid { get; set; } public string name { get; set; } public int pid { get; set; } public List<GetMenuInfos> children { get; set; } } */
如有疑问或者错误的地方,请跟帖,本人会第一时间答复以及相互学习,谢谢!个人会不断的上传自己的学习心得!
好了今天就先到这里,下次有时间再更新,如果存在不合理的地方,欢迎大家多多指教留言!!!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具