将Parent添加到同一对象类型C#的子元素

时间:2014-11-28 09:30:57

标签: c# recursion

我得到的元素包含同一个对象的子列表。

我必须将它们存储在数据库中,然后在相同的结构中将它们检索回来。问题是,我得到的列表没有任何parentId,或任何让我跟踪结构的东西。

我从Web服务获得的元素类似于:

  public class StructureElementFacade
   {

    public int Id { get; set; }

    public string Name { get; set; }

    public List<Structure> Children { get; set; }

    public int ChildCount { get; set; }

    public int Lft { get; set; }

    public int Rgt { get; set; }
   }

它应该被翻译成的元素如下:

  public class Structure
   {
    public int Id { get; set; }
    public string Name { get; set; }

    public List<Structure> Children { get; set; }

    public int ParentId { get; set; }

    public int ChildCount { get; set; }

    public int Lft { get; set; }

    public int Rgt { get; set; }

   }

我试过做类似的事情:

StructureElementFacade Root = Service.GetInstance().GetWebService().getUserStructure(userId, true);

List<Structure> listofstructure = FromStructuresElementsToStructures(Root,new List<Structure>(),Root.Id);

  private List<Structure> FromStructuresElementsToStructures(StructureElementFacade root, List<Structure> list, int parentId )
    {
        var allstructures = list;
        foreach (var child in root.Children)
        {
            if (child.Children.Any())
            {
                FromStructuresElementsToStructures(child,list,child.Id);
            }
            else
            {
                allstructures.Add(
                new Structure
                {
                    Name = child.Name,
                    ParentId = parentId,
                    Lft = child.Lft,
                    Rgt = child.Rgt,
                    ThisId = child.Id,
                });
            }
        }
        return allstructures;
    }

但似乎并没有像我希望的那样奏效。

1 个答案:

答案 0 :(得分:1)

你需要递归,因为孩子也可以生孩子。

List<Structure> convertAndSetParentIds(List<StructureElementFacade> inputItems, int? parentId)
{
     List<Structure> outputItems = new List<Structure>();

     foreach(StructureElementFacade facade in inputItems)
     {
          Structure newStructure = new Structure();
          newStructure.Id = facade.Id;
          newStructure.ParentId = parentId;
          newStructure.Left = facade.lft; 
          //etc... you can figure this bit out.

          // set all the properties based on facade object
          //except for the children, which is where we need recursion.
          newStructure.children = convertAndSetParentIds(facade.children, facade.Id);

          outputItems.add(newStructure);
     }
     return outputItems;
}

现在你可以这样做:

StructureElementFacade Root = Service.GetInstance().GetWebService().getUserStructure(userId, true);

List<Structure> listofstructure = convertAndSetParentIds(new List<StructureElementFacade>(){Root}, null);

listofstructure中的第一个(根)元素将具有NULL的parentId,但其余元素将设置其父ID。

请记住,为了实现这一点,您需要稍微更改Structure对象的结构:

  public class Structure
   {
    public int Id { get; set; }
    public string Name { get; set; }

    public List<Structure> Children { get; set; }

    public int? ParentId { get; set; } //this must be nullable, since the root item has no parent!

    public int ChildCount 
    { 
      get
      {
          //might as well do this:
          return Children == null ? 0 : Children.Count();
      } 
    }

    public int Lft { get; set; }

    public int Rgt { get; set; }

   }