.net list转树状结构
2024-09-02 06:52:46
主要的方法
/// <summary>
/// 转化成树结构
/// </summary>
/// <param name="menuList">菜单的平级list</param>
/// <returns></returns>
private List<AbpMenuModel> GetTreeParent(List<AbpMenuDto> menuList)
{
var dic = new Dictionary<long, AbpMenuModel>(menuList.Count);
foreach (var chapter in menuList)
{
dic.Add(chapter.Id, new AbpMenuModel { Id = chapter.Id, Title = chapter.Title, Path = chapter.Path, ParentId = chapter.ParentId, Icon = chapter.Icon });
}
foreach (var chapter in dic.Values)
{
if (dic.ContainsKey(chapter.ParentId.Value))
{
if (dic[chapter.ParentId.Value].Children == null)
{
dic[chapter.ParentId.Value].Children = new List<AbpMenuModel>(); }
dic[chapter.ParentId.Value].Children.Add(chapter);
}
}
return dic.Values.Where(t => t.ParentId == ).ToList();
}
转换前的类
[AutoMapTo(typeof(AbpMenuDto))]
public class AbpMenuDto : EntityDto<long>
{
/// <summary>
/// 路径
/// </summary>
public string Path { get; set; }
/// <summary>
/// 菜单名
/// </summary>
public string Title { get; set; }
/// <summary>
/// 图标
/// </summary>
public string Icon { get; set; } /// <summary>
/// 父级ID
/// </summary>
public long? ParentId { get; set; }
/// <summary>
/// 类型 菜单(menu) 还是 元素(doc)(按钮等)
/// </summary>
public string Type { get; set; }
/// <summary>
/// doc时 的父级下唯一代码
/// </summary>
public string Code { get; set; } /// <summary>
/// doc时 的名称
/// </summary>
public string Name { get; set; }
}
转换后的类
public class AbpMenuModel : EntityDto<long>
{
/// <summary>
/// 路径
/// </summary>
public string Path { get; set; }
/// <summary>
/// 菜单名
/// </summary>
public string Title { get; set; }
/// <summary>
/// 图标
/// </summary>
public string Icon { get; set; } /// <summary>
/// 父级ID
/// </summary>
public long? ParentId { get; set; }
/// <summary>
/// 类型 菜单(menu) 还是 元素(doc)(按钮等)
/// </summary>
public string Type { get; set; }
/// <summary>
/// doc时 的父级下唯一代码
/// </summary>
public string Code { get; set; } /// <summary>
/// doc时 的名称
/// </summary>
public string Name { get; set; } public List<AbpMenuModel> Children { get; set; }
最新文章
- [LeetCode] Range Sum Query 2D - Mutable 二维区域和检索 - 可变
- 10 Symbol
- MySQL存储过程中实现回滚
- 处理xml c#
- 最近开发的ECG项目
- Hive 按某列的部分排序 以及 删列操作
- Linux常用服务部署与优化之Samba篇
- HDU 1429 (BFS+记忆化状压搜索)
- C118 免按开机自动加载固件
- wikioi 1204 寻找子串位置
- Windows系统性能提升方法
- Java笔记(十九)&hellip;&hellip;多线程
- windows下使用waveout函数族播放wav文件
- 201521123108 《Java程序设计》第13周学习总结
- CodeForces839-B. Game of the Rows-水题(贪心)
- [LeetCode] Smallest Good Base 最小的好基数
- 网卡的 Ring Buffer 详解
- 数据定义语言(DDL Data Definition Language)基础学习笔记
- 学生月上网时间分布-TestData
- Alpha冲刺8