特定のポジションの親部門を選択する必要があるという要件があります。私はテーブルを持っています
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 の直接の親である場合、それを見つけて修正することができますが、ポジションが deparment に間接的に親を持っている場合のクエリの書き方です。
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.
お役に立てれば。