Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何基于父子关系返回记录_C#_Linq_Transitive Closure Table - Fatal编程技术网

C# 如何基于父子关系返回记录

C# 如何基于父子关系返回记录,c#,linq,transitive-closure-table,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 { g

我有一个要求,我需要为一个特定的职位选择上级部门。我把桌子放在桌子上

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. 

希望这能有所帮助。

您尝试了什么?播下一些尝试的代码。另外,显示一些您期望的示例输出。在所有情况下,使用简单的父链接将树结构存储在db中可能不是最佳选择。正如您已经注意到的,这使得查找部门定义最接近的祖先有点困难。您可能希望将树结构存储在单独的闭包表中,这将使类似这样的查询更容易。有关更多信息:@RahulSingh plz请检查代码。