SQL Server 如何在ef内核中调用时从存储过程中获取额外列

yqkkidmi  于 2022-11-28  发布在  其他
关注(0)|答案(1)|浏览(128)
ALTER PROCEDURE [dbo].[EmployeeManagers]
@ID INT  
AS  
Begin
SET FMTONLY OFF;
WITH   subordinate AS (  

    SELECT  Id,  
            Name,  
            Surname,
            BirthDate,
            PositionId,
            ManagerId,
            CreatedDate,
            DeletedDate,
            IsDeleted,
            ModifiedDate,
            1 AS [Managing Level]  
    FROM Employees  
    WHERE Id = @ID  
   
    UNION ALL  
   
    SELECT  e.Id,  
            e.Name,  
            e.Surname,
            e.BirthDate,
            e.PositionId,
            e.ManagerId,
            e.CreatedDate,
            e.DeletedDate,
            e.IsDeleted,
            e.ModifiedDate,
           [Managing Level] + 1
    FROM Employees e  
JOIN subordinate s  
ON e.Id=s.ManagerId
)  
   
  
SELECT    
    m.Id ,  
    m.Name,  
    m.Surname,
    m.BirthDate,
    m.PositionId,
    m.ManagerId,
    m.CreatedDate,
    m.DeletedDate,
    m.IsDeleted,
    m.ModifiedDate,
    s.[Managing Level]  
FROM Employees m

JOIN subordinate s 
ON s.ManagerId = m.Id

ORDER BY [Managing Level];
end

这是我的存储过程,它获取雇员经理。它获取所有雇员经理,并给予他们管理级别

public string Name { get; set; }
      
        public string Surname { get; set; }
        
        public DateTime BirthDate { get; set; }

        public int? PositionId { get; set; }
        public Position Position { get; set; }
        public int? ManagerId { get; set; }
        public ICollection<EmployeeDepartments> EmployeeDepartments { get; set; }
        public DateTime CreatedDate { get; set; }
        public DateTime? DeletedDate { get; set; }
        public bool? IsDeleted { get; set; }
        public DateTime? ModifiedDate { get; set; }
    }

这是我的员工实体
当我调用存储过程时,它只返回雇员实体字段,但我还想获得管理级别

public IQueryable<Employee> GetEmployeeManagers(int employeeId)
        {
        
            var parameter = new SqlParameter("@ID", employeeId);
            var managers = _context.Employees.FromSqlRaw("spManagersTable @ID", parameter);
            return managers;
        }

我无法获得管理级别,这是工作,但我还想获得管理级别。我还需要在我的员工实体中获得管理级别

anauzrmj

anauzrmj1#

第一种情况,WITH子句返回ID为@ID的经理和MangerId为@ID的雇员的ID。然后,它选择仅与这些ID匹配的雇员,这些ID具有您可能需要检索的所有列。

WITH subordinate AS (  
    SELECT  Id,  
            1 AS [Managing Level]
    FROM Employees  
    WHERE Id = @ID
   
    UNION ALL  
   
    SELECT  Id,  
            2 AS [Managing Level] 
    FROM Employees
    WHERE ManagerId = @ID AND Id != @ID
    ORDER BY [Managing Level] ASC;
)   
  
SELECT 
    e.Name,  
    e.Surname,
    e.BirthDate,
    e.PositionId,
    e.ManagerId,
    e.CreatedDate,
    e.DeletedDate,
    e.IsDeleted,
    e.ModifiedDate,
    e.[any other column 1],
    e.[any other column 2],
    e....
FROM Employees e
INNER JOIN subordinate s ON e.Id = s.Id
ORDER BY s.[Managing Level] ASC

或者,您可以简单地使用OR运算,返回经理以及经理ID为@ID的雇员:

SELECT 
    Name,  
    Surname,
    BirthDate,
    PositionId,
    ManagerId,
    CreatedDate,
    DeletedDate,
    IsDeleted,
    ModifiedDate,
    [any other column 1],
    [any other column 2],
    ..
FROM Employees
WHERE (ManagerId = @ID or Id = @ID)
ORDER BY CASE WHEN ManagerId = @ID THEN 0 ELSE 1 END ASC

相关问题