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

推荐订阅源

D
Docker
Apple Machine Learning Research
Apple Machine Learning Research
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 三生石上(FineUI控件)
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
Hugging Face - Blog
Hugging Face - Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
M
MIT News - Artificial intelligence
腾讯CDC
B
Blog RSS Feed
H
Help Net Security
J
Java Code Geeks
有赞技术团队
有赞技术团队
Y
Y Combinator Blog
博客园_首页
Last Week in AI
Last Week in AI
博客园 - 【当耐特】
博客园 - Franky
B
Blog
MongoDB | Blog
MongoDB | Blog
博客园 - 叶小钗
Martin Fowler
Martin Fowler

博客园 - 我有我奥妙

【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