csharp: DataRelation objects to represent a parent/child/Level relationship

简介: /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Page_Load
/// <summary>
       ///
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       protected void Page_Load(object sender, EventArgs e)
       {
 
           var sections = new List<Section>
           {
               new Section { Id = 1, Name = "中国", ParentID = 0 },
               new Section { Id = 2, Name = "江西", ParentID = 1 },
               new Section { Id = 3, Name = "江苏", ParentID = 1 },
               new Section { Id = 4, Name = "南京", ParentID = 3 },
               new Section { Id = 5, Name = "南昌", ParentID = 2 },
               new Section { Id = 6, Name = "东湖区", ParentID = 5 },
               new Section { Id = 7, Name = "广东", ParentID = 1 },
               new Section { Id = 8, Name = "深圳", ParentID = 7 },
               new Section { Id = 9, Name = "罗湖区涂聚文", ParentID = 8 }
           };
 
           //sections = sections.OrderBy(x => x.ParentID).ThenBy(x => x.Name).ToList();
           //var stack = new Stack<Section>();
 
           //// Grab all the items without parents
           //foreach (var section in sections.Where(x => x.ParentID == default(int)).Reverse())
           //{
           //    stack.Push(section);
           //    sections.RemoveAt(0);
           //}
 
           //var output = new List<Section>();
           //while (stack.Any())
           //{
           //    var currentSection = stack.Pop();
 
           //    var children = sections.Where(x => x.ParentID == currentSection.Id).Reverse();
 
           //    foreach (var section in children)
           //    {
           //        stack.Push(section);
           //        sections.Remove(section);
           //    }
           //    output.Add(currentSection);
           //}
           //sections = output;
 
           List<MySection> mys = MenuHelper.GetMyMenuCollection(sections);
 
           //ResolveDDL<MySectionMenu>(mys);
 
           for (int i = 0; i < mys.Count; i++)
           {
                
 
               Response.Write(string.Format("ID:{0} ParentID: {1} TreeLevel: {2} Name:{3}<br/>", mys[i].Id, mys[i].ParentID, mys[i].TreeLevel,mys[i].Name));
           }
 
       }
 
 
       /// <summary>
       ///
       /// </summary>
       /// <typeparam name="T"></typeparam>
       /// <param name="mys"></param>
       protected void ResolveDDL<T>(List<T> mys) where T : MyBaseSection, new()
       {
 
           ResolveDDL<T>(mys, -1, true);
       }
       /// <summary>
       ///
       /// </summary>
       /// <typeparam name="T"></typeparam>
       /// <param name="mys"></param>
       /// <param name="currentId"></param>
       protected void ResolveDDL<T>(List<T> mys, int currentId) where T : MyBaseSection, new()
       {
           ResolveDDL<T>(mys, currentId, true);
       }
 
       /// <summary>
       /// 将一个树型结构放在一个下列列表中可供选择
       /// </summary>
       /// <typeparam name="T"></typeparam>
       /// <param name="currentId"></param>
       /// <param name="mys"></param>
       protected void ResolveDDL<T>(List<T> mys, int currentId, bool addRootNode) where T : MyBaseSection, new()
       {
           if (addRootNode)
           {
               // 所有节点的TreeLevel加一,然后添加根节点
               foreach (T my in mys)
               {
                   my.TreeLevel += 1;
               }
               T root = new T();
               root.Name = "--根节点--";
               root.Id = 0;
               root.TreeLevel = 0;
               mys.Insert(0, root);
           }
 
 
           // currentId==-1表示当前节点不存在
           if (currentId != -1)
           {
               // 本节点不可点击(也就是说当前节点不可能是当前节点的父节点)
               // 并且本节点的所有子节点也不可点击,你想如果当前节点跑到子节点的子节点,那么这些子节点就从树上消失了
               bool startChileNode = false;
               int startTreeLevel = 0;
               foreach (T my in mys)
               {
                   if (my.Id == currentId)
                   {
                       startTreeLevel = my.TreeLevel;
                       my.Enabled = false;
                       startChileNode = true;
                   }
                   else
                   {
                       if (startChileNode)
                       {
                           if (my.TreeLevel > startTreeLevel)
                           {
                               my.Enabled = false;
                           }
                           else
                           {
                               startChileNode = false;
                           }
                       }
                   }
               }
           }
       }
 
 
   }
 
   /// <summary>
   /// /
   /// </summary>
   public class Section
   {
       public int Id { get; set; }
       public string Name { get; set; }
       public int ParentID { get; set; }
   }
   /// <summary>
   ///
   /// </summary>
   public class MySection
   {
       public int Id { get; set; }
       public string Name { get; set; }
       public int ParentID { get; set; }
       public int TreeLevel { get; set; }
   }
   /// <summary>
   ///
   /// </summary>
   public class MySectionMenu : MyBaseSection
   {
 
   }
   /// <summary>
   ///
   /// </summary>
   public class MyBaseSection
   {
        public int Id
       {
           get;
           set;
       }
 
       public int ParentId
       {
           get;
           set;
       }
 
       public string Name
       {
           get;
           set;
       }
               /// <summary>
       /// 本菜单在树形结构中层级(从0开始)
       /// </summary>
       public int TreeLevel
       {
           get;
           set;
       }
 
       /// <summary>
       /// 是否可用(默认true)
       /// </summary>
       public bool Enabled
       {
           get;
           set;
       }
 
       /// <summary>
       /// 是否叶子节点(默认false)
       /// </summary>
       public bool IsTreeLeaf
       {
           get;
           set;
       }
 
 
   }
 
 
   /// <summary>
   ///
   /// </summary>
   public class MenuHelper
   {
 
       /// <summary>
       ///
       /// </summary>
       /// <param name="oldMenus"></param>
       /// <returns></returns>
       public static List<MySection> GetMyMenuCollection(List<Section> oldMenus)
       {
           List<MySection> newMenus = new List<MySection>();
           ResolveMenuCollection(oldMenus, newMenus, 0, 0);
 
           return newMenus;
       }
       /// <summary>
       ///
       /// </summary>
       /// <param name="oldMenus"></param>
       /// <param name="newMenus"></param>
       /// <param name="parentId"></param>
       /// <param name="level"></param>
       /// <returns></returns>
       private static int ResolveMenuCollection(List<Section> oldMenus, List<MySection> newMenus, int parentId, int level)
       {
           int count = 0;
           foreach (Section menu in oldMenus)
           {
               if (menu.ParentID == parentId)
               {
                   count++;
 
                   MySection my = new MySection();
                   newMenus.Add(my);
                   my.TreeLevel = level;
                   my.Id = menu.Id;             
                   my.Name = menu.Name;               
                   my.ParentID = menu.ParentID;      
 
 
                   level++;
                   int childCount = ResolveMenuCollection(oldMenus, newMenus, menu.Id, level);     
                   level--;
               }
           }
 
           return count;
       }
   }

显示结果:

ID:1 ParentID: 0 TreeLevel: 0 Name:中国
ID:2 ParentID: 1 TreeLevel: 1 Name:江西
ID:5 ParentID: 2 TreeLevel: 2 Name:南昌
ID:6 ParentID: 5 TreeLevel: 3 Name:东湖区
ID:3 ParentID: 1 TreeLevel: 1 Name:江苏
ID:4 ParentID: 3 TreeLevel: 2 Name:南京
ID:7 ParentID: 1 TreeLevel: 1 Name:广东
ID:8 ParentID: 7 TreeLevel: 2 Name:深圳
ID:9 ParentID: 8 TreeLevel: 3 Name:涂聚文

目录
相关文章
|
SQL Java 数据库连接
使用Mybatis报错:java.lang.NullPointerException排查方案
使用Mybatis报错:java.lang.NullPointerException排查方案
2890 0
使用Mybatis报错:java.lang.NullPointerException排查方案
|
存储 Kubernetes 数据可视化
交叉证书
## 证书4要素 pubkey(公钥) ,有一个其一一对应的私钥,由证书拥有者保存。 subject(名字),可以理解为证书的名字,你可以理解为 网站的域名。 issuer(颁发者的名字),即上级证书的subject。 signature(签名) ,上级证书使用私钥对当前证书进行签名的值。 ## 证书签名 假设 有 3 级 证书 ![image.png](
6241 0
|
9月前
|
人工智能 API 网络安全
零基础零技术闭眼入!阿里云/本地极速部署OpenClaw+免费大模型配置+iMessage接入保姆级教程
对于完全不懂代码、没接触过云服务器的纯新手来说,搭建AI智能体一直被认为是技术门槛极高的事,但OpenClaw(曾用名Clawdbot)彻底打破了这个壁垒。这款开源轻量化AI智能体,无需复杂编程,就能通过自然语言完成文件管理、办公辅助、信息查询、定时提醒等各类任务,搭配阿里云部署,可实现7×24小时不间断稳定运行,再对接阿里云百炼Coding Plan免费大模型,零成本解锁AI核心推理能力,最后快速接入iMessage,用苹果自带短信就能随时随地和AI对话,日常使用极度便捷。
589 2
|
6月前
|
数据采集 人工智能 缓存
如何申请 1688 拍立淘识图接口白名单
1688拍立淘图片搜索接口(1688.item_search_img)为AI视觉增值接口,需企业实名+三证合一资质人工白名单审核。本文详解准入条件、五步申请流程、驳回避坑点及替代方案,助企业高效开通识图能力,支撑ERP选品与供应链寻源。
962 1
|
6月前
|
安全 API 虚拟化
VMware ESX 9.1.0.0100 发布 - 云计算操作系统
VMware ESX 9.1.0.0100(实为ESXi)于2026年6月5日发布,聚焦安全增强与多项新特性:零接触部署、RDMA可观测性、Guest自定义API升级、机密计算(TDX/SEV-SNP正式可用)、Enhanced DirectPath支持AI GPU、内存分层优化等。无功能新增,仅安全改进。(239字)
602 0
VMware ESX 9.1.0.0100 发布 - 云计算操作系统
|
6月前
|
人工智能 搜索推荐 索引
ChatGPT搜索优化和DeepSeek收录:同样的文章不同的引擎怎么搞
ChatGPT偏重Bing索引与微软生态(如GitHub、维基),DeepSeek更青睐中文平台(知乎、CSDN等)。通用GEO策略:首段嵌关键词、强化结构化数据、多平台分发、构建品牌词矩阵。抢抓2027年智能体普及前的关键窗口期。(239字)
|
6月前
|
弹性计算 安全 Linux
阿里云服务器ecs怎么用?注册、创建ECS实例、配置安全组、远程连接部署和管理全流程
阿里云ECS是弹性可扩展的IaaS云服务器。本文详解从注册账号、创建实例(选地域/规格/镜像/网络/付费方式)、配置安全组、远程连接(SSH/RDP),到部署应用、监控备份及释放实例的全流程,助您快速上手云上计算。阿里云服务器ECS官网:https://t.aliyun.com/U/AZBUsA
805 0
|
6月前
|
存储 人工智能 弹性计算
阿里云2核4G服务器最低多少钱?轻量应用服务器9.9元起,云服务器199元起,不同实例活动价格参考
阿里云2核4G服务器因配置实用备受关注,目前最低价格因实例类型而异。轻量应用服务器2核4G抢购价低至9.9元/月(新用户限时抢),年付199元;通用算力型u1实例2核4G面向企业用户199元/年且续费同价,是企业入门首选;经济型e实例2核2G则99元/年,个人企业均可购买。此外还有u2i、c9i等实例覆盖不同性能需求,均有3至6折优惠。用户可根据身份(新用户/企业/个人)、业务场景(AI部署/官网/博客)及长期成本规划,结合阿里云权益中心优惠券实现折上折,选择最优方案。
|
8月前
|
移动开发 前端开发 JavaScript
前端组件库——Wot Design Uni知识点大全(二)
教程来源 http://unbgv.cn Wot Design Uni 是基于 Vue3+TS 的跨平台 uni-app 组件库,提供 70+ 高质量组件。涵盖按钮、单元格、表单(支持链式校验)、弹窗、Toast、虚拟列表及带徽标的 Tabs 等,全面适配小程序/H5/APP,支持暗黑模式与国际化。