本文主要是介绍C# 将List转成树的两种方式(递归、循环),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
声明一个Model类:
public class TreeNode
{
/// <summary>
/// 子id
/// </summary>
public string Id { get; set; }
/// <summary>
/// 父id
/// </summary>
public string ParentId { get; set; }
/// <summary>
/// 名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 子节点
/// </summary>
public List<TreeNode> Children { get; set; }
/// <summary>
/// 无参构造函数
/// </summary>
public TreeNode()
{
Children = new List<TreeNode>();
}
/// <summary>
/// 有参构造函数
/// </summary>
/// <param name="id">子id</param>
/// <param name="name">名称</param>
/// <param name="parentId">父id</param>
public TreeNode(string id, string name, string parentId)
{
this.Id = id;
this.Name = name;
this.ParentId = parentId;
Children = new List<TreeNode>();
}
/// <summary>
/// 有参构造函数
/// </summary>
/// <param name="id">子id</param>
/// <param name="name">名称</param>
/// <param name="parent">父节点</param>
public TreeNode(string id, string name, TreeNode parent)
{
this.Id = id;
this.Name = name;
this.ParentId = parent.Id;
Children = new List<TreeNode>();
}
}
生成一个转换前的list集合:
TreeNode treeNode1 = new TreeNode("1", "山东", "0");
TreeNode treeNode2 = new TreeNode("2", "北京", "0");
TreeNode treeNode3 = new TreeNode("3", "历下区", treeNode1);
TreeNode treeNode4 = new TreeNode("4", "高新区", treeNode1);
TreeNode treeNode5 = new TreeNode("5", "历城区", treeNode1);
TreeNode treeNode6 = new TreeNode("6", "甸柳庄", treeNode3);
TreeNode treeNode7 = new TreeNode("7", "济南长途汽车站东站", treeNode6);
TreeNode treeNode8 = new TreeNode("8", "朝阳区", treeNode2);
TreeNode treeNode9 = new TreeNode("9", "海淀区", treeNode2);
TreeNode treeNode10 = new TreeNode("10", "金盏乡", treeNode8);
List<TreeNode> list = new List<TreeNode>();
list.Add(treeNode1);
list.Add(treeNode2);
list.Add(treeNode3);
list.Add(treeNode4);
list.Add(treeNode5);
list.Add(treeNode6);
list.Add(treeNode7);
list.Add(treeNode8);
list.Add(treeNode9);
list.Add(treeNode10);
递归方式
调用方式:
List<TreeNode> trees1 = TreeHelper.BulidTreeByRecursive(list, new List<TreeNode>(), "0");
实现代码:
/// <summary>
/// 使用递归方法建树
/// </summary>
public static List<TreeNode> BulidTreeByRecursive(List<TreeNode> treeNodes, List<TreeNode> resps, string pID)
{
resps = new List<TreeNode>();
List<TreeNode> tempList = treeNodes.Where(c => c.ParentId == pID).ToList();
for (int i = 0; i < tempList.Count; i++)
{
TreeNode node = new TreeNode();
node.Id = tempList[i].Id;
node.ParentId = tempList[i].ParentId;
node.Name = tempList[i].Name;
node.Children = BulidTreeByRecursive(treeNodes, resps, node.Id);
resps.Add(node);
}
return resps;
}
双层循环
调用方式:
List<TreeNode> trees = TreeHelper.BulidTree(list);
实现代码:
/// <summary>
/// 双层循环
/// </summary>
/// <param name="treeNodes"></param>
/// <returns></returns>
public static List<TreeNode> BulidTree(List<TreeNode> treeNodes)
{
try
{
List<TreeNode> trees = new List<TreeNode>();
foreach (var treeNode in treeNodes)
{
if ("0" == (treeNode.ParentId))
{
trees.Add(treeNode);
}
foreach (var it in treeNodes)
{
if (it.ParentId == treeNode.Id)
{
treeNode.Children.Add(it);
}
}
}
return trees;
}
catch (Exception ex)
{
throw ex;
}
}
这篇关于C# 将List转成树的两种方式(递归、循环)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!