构建层次结构模型C#

时间:2015-11-18 10:33:25

标签: c# entity-framework linq

我的层次结构模型:

public class SearchHierarchyModel : 
   {
    public string RowID { get; set; }
    public string Name { get; set; }
    public string Type { get; set; }
    public IEnumerable<SearchHierarchyModel> Children { get; set; }
   }

假设从实体填充:

ground // entity list of grounds
building // entity list of buildings based on grounds ids
floors // entity list of floors based on building ids

这些我的助手对象:

 var Building = new SearchHierarchyModel();
 var Ground = new SearchHierarchyModel();
 var TopNode = new SearchHierarchyModel();
var GroundList = new List<SearchHierarchyModel>();

这就是假设填充层次结构的循环:

foreach (var gr in ground)
            {
                var BuildingList = new List<SearchHierarchyModel>();
                foreach (var by in building)
                {
                    BuildingList.Add(new SearchHierarchyModel
                    {
                        Name = by.building,
                        Type = "BUI",
                        RowID = by.Id.ToString()
                    });
                    foreach (var fl in floors)
                    {
                        if (by.Id == fl.Bygning_Bygning_id)
                        {
                            var FloorList = new List<SearchHierarchyModel>();
                            FloorList.Add(new SearchHierarchyModel
                            {
                                Name = fl.floor,
                                Type = "FLR",
                                RowID = fl.Id.ToString()
                            });
                            Building.Children = FloorList;
                        }
                    }
                    Ground.Children = BuildingList;
                }
                TopNode.Children = GroundList;
            }
            return TopNode.Children.ToList();
        }

但是这个层次结构的错误是顶级(地面)应该有两个孩子(两个建筑物)并且它现在是空的,并且这个建筑物中的这一个应该有两个孩子(地板)。任何想法是什么错误的循环?

1 个答案:

答案 0 :(得分:0)

您正在将对象的新实例添加到临时列表中,然后将这些列表设置为唯一的实例Building,Ground e.t.c。

你的循环中有一个巨大的指针混乱,

如果我没有误会,以下是您应该执行的代码:

var TopNode = new SearchHierarchyModel();

var groundList = new List<SearchHierarchyModel>();

foreach (var gr in ground)
{
    SearchHierarchyModel newGround = new SearchHierarchyModel()
    {
        Name = gr.ground,
        Type = "GRD",
        RowID = gr.Id.ToString()
    };

    groundList.Add(newGround);

    var buildingList = new List<SearchHierarchyModel>();

    foreach (var by in building)
    {
        SearchHierarchyModel newBuilding = new SearchHierarchyModel()
        {
            Name = by.building,
            Type = "BUI",
            RowID = by.Id.ToString()
        };

        buildingList.Add(newBuilding);

        foreach (var fl in floors)
        {
            if (by.Id == fl.Bygning_Bygning_id)
            {
                var floorList = new List<SearchHierarchyModel>();
                floorList.Add(new SearchHierarchyModel
                {
                    Name = fl.floor,
                    Type = "FLR",
                    RowID = fl.Id.ToString()
                });
                newBuilding.Children = floorList;
            }
        }
        newGround.Children = buildingList;
    }
    TopNode.Children = groundList;
}