1

我有一个要求,我需要为特定职位选择家长部门。我有桌子

public partial class Position
{    
 public int PositionId { get; set; }
 public string PositionName { get; set; }
 public int ParentPositionId { get; set; }
}
public partial class DeptPosition
{
 public long Id { get; set; }
 public int DeptId { get; set; }
 public int PosId { get; set; }
}
public partial class Dept
{
 public int DepartmentId { get; set; }
 public string DepartmentName { get; set; }
}

现在,一个职位可以有一个父级作为另一个职位或部门。假设 Pos1 的父项为 Pos2,Pos2 的父项为 Pos3,而 Pos3 现在的父项为 Dept1。所以在这里我如何通过其他位置获得作为 Pos1 的父级的 Dept1。现在在这里,如果 Dept1 是 Pos1 的直接父级,那么我可以找到并修复它,但是如果某个职位间接在部门中有父级,如何编写查询。

Example:1

Department
DepartmentId       DepartmentName
----------------------------------------------
1                  HR
2                  IT

Position
--------------------------------------------------------
PositionId         PositionName    ParentPositionId
1001               Pos1            1002  //Pos2
1002               Pos2            1003  //Pos3
1003               Pos3            null
1004               Pos4            null

DeptPos
Id                 DeptId          PosId
----------------------------------------
2001               1               1003
2002               2               1004     

感谢所有回复。现在说如果我想获得 Pos3 的家长部门。那我就简单的说

var parId=db.DeptPos.FirstOfDefault(x=>x.PositionId==id).DeptId;
var parName=db.Department.FirstOfDefault(x=>x.DepartmentId==parId).DepartmentName;

Here id for the position is 1003

现在这将给该职位的家长部门并且由于 Pos1 没有作为直接家长的部门所以现在我说

 Example:2    
Here the id for the position is 1001
var parId=db.DeptPos.FirstOfDefault(x=>x.PositionId==id).DeptId;
Here I get parId as null since Pos1 with Id 1001 doesnot a department as direct parent.
if(parId==null)
{
 var parPosId=db.Positions.FirstOrDefault(x=>x.PositionId==id).ParentPositionId
 //Getting ParentPositionRecord
 var parPosId2=db.Positions.FirstOrDefault(x=>x.PositionId==parPosId).ParentPositionId;
 //Now if there are 20 positions and let's say if that position is having a parent Department via 10 positions. An example here
}
Position
--------------------------------------------------------
PositionId         PositionName    ParentPositionId
1001               Pos1            1002  
1002               Pos2            1003  
1003               Pos3            1004
1004               Pos4            1005
1005               Pos5            1006  
1006               Pos6            1007  
1007               Pos7            1008  
1008               Pos8            1009  
1009               Pos9            1010 
1010               Pos10           null  
----               ----            ------
1020               Pos20           null 

DeptPos
Id                 DeptId          PosId
----------------------------------------
2001               1               1010
2002               2               1020
I need to go on repeating code 10 times or might be ...........
So is there something which gives me DeptId as 1 from DeptPos in above  example by some other way. 

希望这可以帮助。

4

0 回答 0