部门树的数据结构如下:
部门本身就是一个List<DepartmentOutDto>,每个部门下无限个Children
实体类:
根据部门id获取部门名称:
#region 递归,根据部门id获取树结构下的部门名称
/// <summary>
/// 递归,根据部门id获取树结构下的部门名称
/// </summary>
/// <param name="departmentList">部门列表</param>
/// <param name="departmentId">需要查询的部门id</param>
/// <param name="departmentName">部门id对应的部门名称</param>
private void GetDepartmentById(List<Csg.NuGet.BbsClient.DepartmentOutDto> departmentList, string departmentId, out string departmentName)
{
departmentName = string.Empty;
for (int i = 0; i < departmentList.Count; i++)
{
if (departmentList[i].Id.ToString() == departmentId)
{
departmentName = departmentList[i].Name;
break;
}
else
{
if (departmentList[i].Children != null)
{
List<Csg.NuGet.BbsClient.DepartmentOutDto> childDepartment = departmentList[i].Children.ToList();
GetDepartmentById(childDepartment, departmentId, out departmentName);
}
else
{
continue;
}
}
}
}
#endregion 递归,根据部门id获取树结构下的部门名称