title: Updating Hierarchical Numbering
body: <p>I have below classes in my application which holds hierarchical data as below:</p>
  
  <pre><code>Class A 
  {
     string Id{get;set;}
     string Name{get;set;}
     string Order{get;set;}
     string ParentId{get;set;}
  }
  Class B 
  {
     string Id{get;set;}
     string Name{get;set;}
     string Order{get;set;}
     string ClassAId{get;set;}
  }
  Class C 
  {
     string Id{get;set;}
     string Name{get;set;}
     string Order{get;set;}
     string ClassBId{get;set;}
  }
  </code></pre>
  
  <p>Sample data for class A would be </p>
  
  <pre><code>Id = "1.1"                   Id = "2.1"
  Name = "SomeName"            Name = "Name2"
  Order = 1                    Order = 1
  ParentId = "1"               ParentId = 2
  
  Id = "1.2"                   Id = "2.2"
  Name = "Name2"               Name = "Name3"
  Order = 2                    Order = 2
  ParentId = "1"               ParentId = 2
  </code></pre>
  
  <p>Sample data for Class B would be</p>
  
  <pre><code> Id = "1.1.1"                   Id = "2.1.1"
      Name = "SomeName"            Name = "Name2"
      Order = 1                    Order = 1
      ParentId = "1.1"               ParentId = 2.1
  
      Id = "1.2.1"                   Id = "2.1.2"
      Name = "Name2"               Name = "Name3"
      Order = 2                    Order = 2
      ParentId = "1.2"               ParentId = 2.1
  </code></pre>
  
  <p>Similarly for Class C, data would look like </p>
  
  <pre><code>Id = "1.1.1.1"                   Id = "2.1.1.1"
  Name = "SomeName"            Name = "Name2"
  Order = 1                    Order = 1
  ParentId = "1.1.1"               ParentId = 2.1.1
  </code></pre>
  
  <p>Now if user wants to update a value in between the existing values, the way it should work is, suppose I am entering a value between 1.2 and 1.3 for Class A, it should first create a new value called 1.4, then move all the contents of 1.3 and its children to 1.4 (i.e, if 1.3 has its own children like 1.3.1 so on and 1.3.1.1 so on all should be renamed to 1.4.1 and 1.4.1.1 respectively and 1.3 should be free of any hierarchy with empty value. Basically in between inserts should update the complete hierarchy of a record. I am able to generate next sequence correctly by finding current Id and finding maximum of order and adding 1 to it. Issue I am facing is in between inserts and updating whole hierarchy. 
  Any ideas would help. Below is the code I have written for entering a new value Class A :</p>
  
  <p>//Find the max order, increment it by 1 and insert the new record. </p>
  
  <pre><code>var currentClass = listOfClassA.Where(x =&gt; x.Id = currentId).SingleOrDefault();
  var maxOrder = listOfClassA.Max(x =&gt; x.Order);
  var objClassA = new A();
  objClassA.Order = maxOrder + 1;
  objClassA.ParentId = currentClassA.ParentId;
  objClassA.Name = "";
  objClassA.Id = currentClassA.ParentId + "." + objClassA.Order;
  </code></pre>