首页 > 其他分享 >大家来找茬---树的遍历

大家来找茬---树的遍历

时间:2022-12-12 12:11:18浏览次数:39  
标签:遍历 Childrens List --- item func null public 大家来找

 1         /// <summary>
 2         /// 给定一个组织Id,查找出这个组织的树结构(含岗位)
 3         /// </summary>
 4         /// <param name="organizationId">组织Id</param>
 5         /// <returns>结果</returns>
 6         public async Task<WebOrganizationTreeOutput> GetChilderPostion(string organizationId)
 7         {
 8             // 查找全部子集的岗位
 9             var organizationList = await WebOrganizationListSearch();
10             Func<string, List<WebOrganizationTreeOutput>, WebOrganizationTreeOutput> func = null;
11             func = (organizationId, list) =>
12             {
13                 if (list == null)
14                 {
15                     return null;
16                 }
17 
18                 foreach (var item in list)
19                 {
20                     if (item.OrganizationId == organizationId)
21                     {
22                         return item;
23                     }
24                     else if(item.OrganizationLevel > 0 && item.Childrens != null && item.Childrens.Count > 0)
25                     {
26                         return func(organizationId, item.Childrens);
27                     }
28                 }
29 
30                 return null;
31             };
32 
33             return func(organizationId, organizationList);
34         }

 

数据的结构

 1     /// <summary>
 2     /// 组织架构目录
 3     /// </summary>
 4     public class WebOrganizationTreeOutput
 5     {
 6         /// <summary>
 7         /// Id
 8         /// </summary>
 9         public string OrganizationId { get; set; }
10 
11         /// <summary>
12         /// 组织名称
13         /// </summary>
14         public string OrganizationName { get; set; }
15 
16         /// <summary>
17         /// 组织架构级别,默认从1开始
18         /// </summary>
19         public int OrganizationLevel { get; set; }
20 
21         /// <summary>
22         /// 代表数量
23         /// </summary>
24         public int UserCount { get; set; }
25 
26         /// <summary>
27         /// 父级组织架构ID
28         /// </summary>
29         public string? ParentOrganizationId { get; set; }
30 
31         /// <summary>
32         /// 下级
33         /// </summary>
34         public List<WebOrganizationTreeOutput> Childrens { get; set; }
35 
36         /// <summary>
37         /// 组织岗位
38         /// </summary>
39         public List<WebOrganizationPositionOutput> Position { get; set; }
40 
41         /// <summary>
42         /// 获取树的全部岗位
43         /// </summary>
44         /// <returns>结果</returns>
45         public List<string> GetTreeAllPosition()
46         {
47             List<string> list = new List<string>();
48             if (Position != null)
49             {
50                 list.AddRange(Position.Select(p => p.PositionId));
51             }
52 
53             Func<List<WebOrganizationTreeOutput>, List<string>> func = null;
54             func = (input) =>
55             {
56                 List<string> result = new List<string>();
57                 foreach (var item in input)
58                 {
59                     if (item.Position != null)
60                     {
61                         result.AddRange(item.Position.Select(p => p.PositionId));
62                     }
63 
64                     if (item.Childrens != null)
65                     {
66                         result.AddRange(func(item.Childrens));
67                     }
68                 }
69 
70                 return result;
71             };
72 
73             if (Childrens != null)
74             {
75                 list.AddRange(func(Childrens));
76             }
77 
78             return list;
79         }
80     }

 

标签:遍历,Childrens,List,---,item,func,null,public,大家来找
From: https://www.cnblogs.com/lsgControl/p/16975703.html

相关文章