【问题标题】:How to write a get api to obtain employee hierarchy till CEO for a specific employee如何编写一个get api来获取特定员工的员工层次结构直到CEO
【发布时间】:2021-11-16 19:03:40
【问题描述】:

我想编写一个 api 来获取从员工到 CEO 的整个经理层级 例如在下表中-

EmployeeName    ManagerName
C                 D
B                 C
G                 C
E                 B
F                 B
A                 B
D                 NULL

对于员工 A,我希望输出为 BCD

我正在使用 ASP.NET Core Web API,SQL Server 也一样。

我尝试将代码编写为-

[Route("empLevel/{id}")]
        public IActionResult GetEmpHierarchy(int id)
        {
           List<Employee> emp = entity.Employees.ToList();
           List<Employee> mngr = entity.Employees.ToList();

            var query = (from e in emp
                         join m in mngr on e.MngId equals m.Id
                         select new Employee { Id = e.Id, MngId = m.MngId }).Where(x => x.Id == id).ToList();

            return Ok(query);
        }

但结果仍然不如预期。请提供解决方案。

【问题讨论】:

  • 尝试将 where 子句放在 from 语句中

标签: c# entity-framework asp.net-core asp.net-web-api get


【解决方案1】:

您需要一个递归操作来从当前位置导航到顶层。此外,您可以使用while-loop 来实现该解决方案,并每次都替换当前节点。

要编写代码,需要知道员工/经理如何与其他人联系起来,在这种情况下似乎是MngId

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-20
    相关资源
    最近更新 更多