title: Adding ParentId to child element of same object type C#
body: <p>I am getting an element that contains a list of childs, of the same object.</p>
  
  <p>I have to store them in a Database, and later on retrieve them back in the same structure. The problem is, that the list I get does not have any parentId, or anything that lets me keep track on the structure.</p>
  
  <p>The element I get from the web service looks something like:</p>
  
  <pre><code>  public class StructureElementFacade
     {
  
      public int Id { get; set; }
  
      public string Name { get; set; }
  
      public List&lt;Structure&gt; Children { get; set; }
  
      public int ChildCount { get; set; }
  
      public int Lft { get; set; }
  
      public int Rgt { get; set; }
     }
  </code></pre>
  
  <p>And the element it should be translated to looks like:</p>
  
  <pre><code>  public class Structure
     {
      public int Id { get; set; }
      public string Name { get; set; }
  
      public List&lt;Structure&gt; Children { get; set; }
  
      public int ParentId { get; set; }
  
      public int ChildCount { get; set; }
  
      public int Lft { get; set; }
  
      public int Rgt { get; set; }
  
     }
  </code></pre>
  
  <p>I have tried to do something like:</p>
  
  <pre><code>StructureElementFacade Root = Service.GetInstance().GetWebService().getUserStructure(userId, true);
  
  List&lt;Structure&gt; listofstructure = FromStructuresElementsToStructures(Root,new List&lt;Structure&gt;(),Root.Id);
  
    private List&lt;Structure&gt; FromStructuresElementsToStructures(StructureElementFacade root, List&lt;Structure&gt; 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;
      }
  </code></pre>
  
  <p>But does not seem to work as I hoped.</p>