- A+
所属分类:.NET技术
本章将和大家分享C#中如何通过递归获取所有的子分类。废话不多说,我们直接来看一下Demo,如下所示:
递归写法如下:
using System; using System.Collections.Generic; using System.Linq; namespace RecursionDemo { /// <summary> /// 树节点 /// </summary> public class TreeNode { /// <summary> /// 节点Id /// </summary> public int Id { get; set; } /// <summary> /// 节点名称 /// </summary> public string Name { get; set; } /// <summary> /// 父节点Id /// </summary> public int ParentId { get; set; } /// <summary> /// 子节点 /// </summary> public List<TreeNode> Children { get; set; } } /// <summary> /// 树节点业务逻辑 /// </summary> public class TreeNodeBLL { /// <summary> /// 通过递归获取所有子孙节点 /// </summary> /// <param name="parentId">父节点Id</param> /// <param name="listAllTreeNode">所有节点数据</param> /// <returns>返回所有子孙节点</returns> public List<TreeNode> GetChildren(int parentId, List<TreeNode> listAllTreeNode) { var listResult = new List<TreeNode>(); //存放返回结果的集合 var listChildren = listAllTreeNode.FindAll(node => node.ParentId == parentId); if (listChildren.Count <= 0) { //此处是返回 null 值,还是要返回 listResult ,则需要根据具体的业务逻辑来决定 return null; //return listResult; } //遍历指定parentId下的第一级子节点 foreach (var item in listChildren) { var childNode = new TreeNode(); childNode.Id = item.Id; childNode.Name = item.Name; childNode.ParentId = item.ParentId; childNode.Children = GetChildren(item.Id, listAllTreeNode); //递归获取子节点下的子节点 listResult.Add(childNode); } return listResult; } /// <summary> /// 初始化级联数据(用于演示) /// </summary> /// <returns></returns> public List<TreeNode> InitData() { //级联数据,此处以省市区级联数据为例 var listAllTreeNode = new List<TreeNode>() { new TreeNode() { Id = 1, ParentId = 0, Name = "福建省" }, new TreeNode() { Id = 2, ParentId = 0, Name = "上海市" }, new TreeNode() { Id = 3, ParentId = 1, Name = "福州市" }, new TreeNode() { Id = 4, ParentId = 1, Name = "厦门市" }, new TreeNode() { Id = 5, ParentId = 3, Name = "永泰县" }, new TreeNode() { Id = 6, ParentId = 2, Name = "黄浦" }, new TreeNode() { Id = 7, ParentId = 2, Name = "虹口" }, new TreeNode() { Id = 8, ParentId = 2, Name = "宝山" } }; return listAllTreeNode; } } }
调用如下:
using Newtonsoft.Json; namespace RecursionDemo { /// <summary> /// 递归Demo /// </summary> internal class Program { static void Main(string[] args) { //示例1:C#中通过递归获取所有的子分类 var treeNodeBLL = new TreeNodeBLL(); var listAllTreeNode = treeNodeBLL.InitData(); var childrenNode = treeNodeBLL.GetChildren(0, listAllTreeNode); var childrenNodeJson = JsonConvert.SerializeObject(childrenNode); //序列化成Json字符串 Console.WriteLine(childrenNodeJson); //换行 Console.WriteLine(); //示例2:求 斐波那契数列 第n项是多少?(通过递归方式获取) int n = 8; //第8项 int result = GetFibonacciSequence(n); Console.WriteLine(string.Format(@"斐波那契数列 第{0}项是:{1}", n, result)); Console.ReadKey(); } /// <summary> /// 获取 斐波那契数列 第n项的值 (通过递归方式获取) /// </summary> /// <param name="n">第n项</param> /// <returns>第n项的值</returns> static int GetFibonacciSequence(int n) { //斐波那契数列指的是这样一个数列:1,1,2,3,5,8,13,21,34,55,89... //这个数列从第3项开始,每一项都等于前两项之和。 if (n < 3) { return 1; } return GetFibonacciSequence(n - 1) + GetFibonacciSequence(n - 2); //通过递归获取前两项之和 } } }
运行结果如下所示:
其中 childrenNodeJson 值格式化后结果如下所示:
[ { "Id":1, "Name":"福建省", "ParentId":0, "Children":[ { "Id":3, "Name":"福州市", "ParentId":1, "Children":[ { "Id":5, "Name":"永泰县", "ParentId":3, "Children":null } ] }, { "Id":4, "Name":"厦门市", "ParentId":1, "Children":null } ] }, { "Id":2, "Name":"上海市", "ParentId":0, "Children":[ { "Id":6, "Name":"黄浦", "ParentId":2, "Children":null }, { "Id":7, "Name":"虹口", "ParentId":2, "Children":null }, { "Id":8, "Name":"宝山", "ParentId":2, "Children":null } ] } ]
Demo源码:
链接:https://pan.baidu.com/s/16PpxARzJbwNUeU26hJi0MA 提取码:sezs
此文由博主精心撰写转载请保留此原文链接:https://www.cnblogs.com/xyh9039/p/17419432.html
版权声明:如有雷同纯属巧合,如有侵权请及时联系本人修改,谢谢!!!