如何根据父子关系返回记录

时间:2015-05-09 15:04:56

标签: c# linq transitive-closure-table

我有一个要求,我需要为特定职位选择家长部门。我把表格作为

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. 

希望这有帮助。

0 个答案:

没有答案