【发布时间】:2019-12-25 01:34:24
【问题描述】:
我有一些数据代表同一实体上的父子关系。给定一个节点,我需要找到它的整个上层(父、祖父、曾祖父等)
我的实体是这样的:
public partial class Location{
public int LocationId { get; set; }
public int? FkParentLocationId { get; set; }
..... more properties here.......
public virtual Location FkParentLocation { get; set; }
public virtual ICollection<Location> InverseFkParentLocation { get; set; }
}
我指的是建议的层次结构遍历实现here,但是当你沿着层次结构向下时它会起作用。如何使用 LINQ 检索上层层次结构?
样本数据:
List<Location> locations = new List<Location> {
new Location { LocationId = 5, FkParentLocationId = 3, LocationName = "Windsor", LocationDisplayName = "Windsor"},
new Location { LocationId = 15, FkParentLocationId = 3, LocationName = "Hampshire", LocationDisplayName = "Hampshire" },
new Location { LocationId = 12, FkParentLocationId = 3, LocationName = "Sussex", LocationDisplayName = "Sussex"},
new Location { LocationId = 13, FkParentLocationId = 3, LocationName = "Willowood", LocationDisplayName = "Willowood"},
new Location { LocationId = 1, FkParentLocationId = 3, LocationName = "Gerbshire", LocationDisplayName = "Gerbshire"},
new Location { LocationId = 3, FkParentLocationId = 2, LocationName = "Lincoln", LocationDisplayName = "Lincoln"},
new Location { LocationId = 2, LocationName = "Mains", LocationDisplayName = "Mains" } };
预期输出:给定位置 ID:5,我应该得到一个包含位置 3 和 2 的列表(因为它们是父位置)。
【问题讨论】:
-
您能提供示例数据吗?
-
你能提供更多关于你想要达到的目标的细节吗?您发布的示例数据示例,输入 - 输出
-
在问题中添加了请求的详细信息。
标签: linq parent-child hierarchical-data ef-core-3.0