惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

TaoSecurity Blog
TaoSecurity Blog
Jina AI
Jina AI
雷峰网
雷峰网
月光博客
月光博客
The GitHub Blog
The GitHub Blog
WordPress大学
WordPress大学
B
Blog RSS Feed
美团技术团队
C
CXSECURITY Database RSS Feed - CXSecurity.com
小众软件
小众软件
Security Latest
Security Latest
Microsoft Azure Blog
Microsoft Azure Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cybersecurity and Infrastructure Security Agency CISA
Last Week in AI
Last Week in AI
A
Arctic Wolf
Latest news
Latest news
Attack and Defense Labs
Attack and Defense Labs
I
Intezer
F
Fortinet All Blogs
罗磊的独立博客
MongoDB | Blog
MongoDB | Blog
Webroot Blog
Webroot Blog
S
Secure Thoughts
Help Net Security
Help Net Security
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
V
Visual Studio Blog
P
Proofpoint News Feed
博客园 - 【当耐特】
P
Privacy International News Feed
V
Vulnerabilities – Threatpost
Stack Overflow Blog
Stack Overflow Blog
Know Your Adversary
Know Your Adversary
云风的 BLOG
云风的 BLOG
Hacker News: Ask HN
Hacker News: Ask HN
L
LINUX DO - 最新话题
H
Help Net Security
爱范儿
爱范儿
酷 壳 – CoolShell
酷 壳 – CoolShell
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
T
Tailwind CSS Blog
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tenable Blog
Cloudbric
Cloudbric
N
News and Events Feed by Topic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog

博客园 - 我有我奥妙

【BenchmarkDotNet】测试多方式的对象映射 【自动注入】.NET8/.NETCore 依赖注入:自动注入项目中所有接口和自定义类 【排名】处理同分数的排名 【Quartz】.Net8使用定时任务 【模型验证】未被异常捕获到 【Ant Design Vue】相关 【C#】枚举值 【ECharts】图表自定义显示标题 【消息队列】介绍 【Nginx】Windows部署Vue 设计模式(一)-介绍 【.NetCore】创建本机的静态文件服务器 NLog(一)-使用示例 【nssm】windows上netcore注册为服务 【字符串排序】C#和前端js排序问题 【长路经】C#读取文件抛出FileNotFoundException异常 【RestSharp】常用的几个请求方式 【笔记软件】Obsidian的使用 【浏览器扩展】编写Firefox和Chrome的扩展程序
【根节点】C#找树形数据的根节点Id
我有我奥妙 · 2025-04-13 · via 博客园 - 我有我奥妙

科室类

    public class Dept
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string ParentId { get; set; }
        public string RootId { get; set; }
    }

测试代码

        static void Test07()
        {
            List<Dept> depts = new List<Dept>();
            depts.Add(new Dept { Id = "01", Name = "部门01", ParentId = "", RootId = "", });
            depts.Add(new Dept { Id = "02", Name = "部门02", ParentId = "", RootId = "", });
            depts.Add(new Dept { Id = "03", Name = "部门03", ParentId = "01", RootId = "", });
            depts.Add(new Dept { Id = "04", Name = "部门04", ParentId = "01", RootId = "", });
            depts.Add(new Dept { Id = "05", Name = "部门05", ParentId = "04", RootId = "", });
            depts.Add(new Dept { Id = "06", Name = "部门06", ParentId = "05", RootId = "", });

            foreach (var dept in depts)
            {
                if (string.IsNullOrEmpty(dept.ParentId))
                {
                    dept.RootId = dept.Id;
                }
                else
                {
                    dept.RootId = FindRootId(depts, dept);
                }
            }

            // 输出结果,查看RootId的赋值情况
            foreach (var dept in depts)
            {
                Console.WriteLine($"Id: {dept.Id}, Name: {dept.Name}, ParentId: {dept.ParentId}, RootId: {dept.RootId}");
            }
        }

        private static string FindRootId(List<Dept> depts, Dept currentDept)
        {
            string parentId = currentDept.ParentId;
            while (!string.IsNullOrEmpty(parentId))
            {
                var parentDept = depts.Find(d => d.Id == parentId);
                if (string.IsNullOrEmpty(parentDept.ParentId))
                {
                    return parentDept.Id;
                }
                parentId = parentDept.ParentId;
            }
            return currentDept.Id; // 如果没有找到父部门,则返回自身Id
        }

运行结果

Id: 01, Name: 部门01, ParentId: , RootId: 01
Id: 02, Name: 部门02, ParentId: , RootId: 02
Id: 03, Name: 部门03, ParentId: 01, RootId: 01
Id: 04, Name: 部门04, ParentId: 01, RootId: 01
Id: 05, Name: 部门05, ParentId: 04, RootId: 01
Id: 06, Name: 部门06, ParentId: 05, RootId: 01
ok