我有一个包含子订单等的订单列表。它们通过childorderid和orderid链接。但是,在每个订单中都有通过pid和parentpid链接的产品和子产品。然而,并不是每个产品都有子产品。
我附上了db中数据的示例屏幕截图,以说明我的意思和我希望实现的目标。我遇到的问题是,我需要将这个平面文件结构放入一个嵌套的c#列表中。每个bomlineclass都有一个bomlineclass的列表
var navigationItems = bomline.Select(
i => new BOMLineClass
{
ParentOrderID = i.ParentOrderID,
BSOOrderNo = i.BSOOrderNo,
BSODemandDate = i.BSODemandDate,
OrderTypeDesc = i.OrderTypeDesc,
OrderType = i.OrderType,
BuildRef = i.BuildRef,
HasSubAssembly = i.HasSubAssembly,
ChildOrderID= i.ChildOrderID,
ChildOrderNo= i.ChildOrderNo,
OrderID = i.OrderID,
OrderLineID = i.OrderLineID,
ProductCode = i.ProductCode,
ParentPID = i.ParentPID,
PID = i.PID,
}
).ToList();
foreach (var i in navigationItems)
{
i.BOMLines = navigationItems.Where(n => n.ChildOrderID== i.OrderID).ToList();
foreach (var x in i.BOMLines)
{
//Thought I could link the children via the product ID and parent product Id here
}
}
List<BOMLineClass> rootNavigationItems2 = navigationItems.Where(n => n.ChildOrderID == false).ToList();
bh.BOMLines = rootNavigationItems2;
我已经挣扎了至少5天了。
1条答案
按热度按时间6rqinv9w1#
看看下面是否有用。您必须从childorderid为null的leaf开始,一直到root。您还需要有兄弟节点和子节点。参见下面的代码