使用 c# 在 json 中获取分层输出 --> asp.net web api
get hierarchical output in json using c# --> asp.net web api
我想使用 C#(ASP.NET Web Api) 在 JSON 输出中创建员工和经理的层次结构树。我使用了 EntityFramework。
我得到的api代码是-
[Route("empLevel/{id}")]
public IActionResult GetEmpHierarchy(int id)
{
try
{
if (id == 0)
{
return NotFound();
}
var query_hierarchy = entity.EmployeeHierarchies.FromSqlInterpolated($"EXECUTE dbo.FindHierarchy {id}").ToList();
return Ok(query_hierarchy);
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
return NotFound();
}
class EmployeeHierarchy 的代码是-
public class EmployeeHierarchy
{
public int Id { get; set; }
public int? MngId { get; set; }
public string Path { get; set; }
public string Name { get; set; }
public string FullPath { get; set; }
public int Level { get; set; }
public double Grade { get; set; }
}
我在从数据库获取输出的过程中使用了 CTE,它是-
ALTER PROCEDURE [dbo].[FindHierarchy]
@empId int = null
AS
BEGIN
with cte as(
select Id, MngId, CAST('' AS VARCHAR(40)) ManagerName, Path = cast('' as varchar(40)),Name, Level = 0,Grade
from Employee
where MngId is null
union all
select EmpId = e.Id, ParentId = e.MngId,
CAST(cte.Name AS VARCHAR(40)) ManagerName,
Path = cast(concat(cte.Path, cte.Name, ' -› ') as varchar(40)),
Name = e.Name,
Level = cte.Level + 1,
Grade = e.Grade
from
Employee e
inner join cte on cte.Id = e.MngId
)
select
Id ,
MngId ,
ManagerName,
Path ,
Name ,
FullPath = Path + Name,
Level, Grade
from cte where ID=@empId
order by FullPath ;
END
目前我的输出是:
[
{
"id":2,
"mngId":3,
"name":"Blake",
"fullPath":"Diana › Chelsa › Blake",
"level":2,
"grade":3.2
}
]
我想显示为-
[
{
"id":2,
"mngId":3,
"name":"Diana",
"level":2,
"grade":5.2[
{
"id":2,
"mngId":3,
"name":"Chelsa",
"level":2,
"grade":5.1[
{
"id":2,
"mngId":3,
"name":"Diana",
"level":2,
"grade":3.2[]
}
]
}
]
}
]
我是 asp.net 网络 api 的新手,请为我的查询提供一些代码。谢谢!!
您的员工层次结构应如下所示
public class EmployeeHierarchy
{
public int Id { get; set; }
public int? MngId { get; set; }
public string Path { get; set; }
public string Name { get; set; }
public string FullPath { get; set; }
public int Level { get; set; }
public double Grade { get; set; }
public EmployeeHierarchy ChildEmployee { get; set; }
}
这是我能看到的唯一方法。
您必须编辑您的 SQL 以匹配此型号。
我想使用 C#(ASP.NET Web Api) 在 JSON 输出中创建员工和经理的层次结构树。我使用了 EntityFramework。
我得到的api代码是-
[Route("empLevel/{id}")]
public IActionResult GetEmpHierarchy(int id)
{
try
{
if (id == 0)
{
return NotFound();
}
var query_hierarchy = entity.EmployeeHierarchies.FromSqlInterpolated($"EXECUTE dbo.FindHierarchy {id}").ToList();
return Ok(query_hierarchy);
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
return NotFound();
}
class EmployeeHierarchy 的代码是-
public class EmployeeHierarchy
{
public int Id { get; set; }
public int? MngId { get; set; }
public string Path { get; set; }
public string Name { get; set; }
public string FullPath { get; set; }
public int Level { get; set; }
public double Grade { get; set; }
}
我在从数据库获取输出的过程中使用了 CTE,它是-
ALTER PROCEDURE [dbo].[FindHierarchy]
@empId int = null
AS
BEGIN
with cte as(
select Id, MngId, CAST('' AS VARCHAR(40)) ManagerName, Path = cast('' as varchar(40)),Name, Level = 0,Grade
from Employee
where MngId is null
union all
select EmpId = e.Id, ParentId = e.MngId,
CAST(cte.Name AS VARCHAR(40)) ManagerName,
Path = cast(concat(cte.Path, cte.Name, ' -› ') as varchar(40)),
Name = e.Name,
Level = cte.Level + 1,
Grade = e.Grade
from
Employee e
inner join cte on cte.Id = e.MngId
)
select
Id ,
MngId ,
ManagerName,
Path ,
Name ,
FullPath = Path + Name,
Level, Grade
from cte where ID=@empId
order by FullPath ;
END
目前我的输出是:
[
{
"id":2,
"mngId":3,
"name":"Blake",
"fullPath":"Diana › Chelsa › Blake",
"level":2,
"grade":3.2
}
]
我想显示为-
[
{
"id":2,
"mngId":3,
"name":"Diana",
"level":2,
"grade":5.2[
{
"id":2,
"mngId":3,
"name":"Chelsa",
"level":2,
"grade":5.1[
{
"id":2,
"mngId":3,
"name":"Diana",
"level":2,
"grade":3.2[]
}
]
}
]
}
]
我是 asp.net 网络 api 的新手,请为我的查询提供一些代码。谢谢!!
您的员工层次结构应如下所示
public class EmployeeHierarchy
{
public int Id { get; set; }
public int? MngId { get; set; }
public string Path { get; set; }
public string Name { get; set; }
public string FullPath { get; set; }
public int Level { get; set; }
public double Grade { get; set; }
public EmployeeHierarchy ChildEmployee { get; set; }
}
这是我能看到的唯一方法。 您必须编辑您的 SQL 以匹配此型号。