title: Add sequence number for dynamic hierarchy in C#
body: <p>Suppose I have these records:</p>
  
  <pre><code>Code     |GroupLevel    |Group
  -----------------------------------
  X0000    |4             |
  X1000    |3             |X0000
  X2000    |3             |X0000
  X3000    |3             |X0000
  X1100    |2             |X1000
  X1200    |2             |X1000
  X1300    |2             |X1000
  X2100    |2             |X2000
  X2200    |2             |X2000
  X2300    |2             |X2000
  X1110    |1             |X1100
  X1120    |1             |X1100
  X1111    |0             |X1110
  X1112    |0             |X1110
  X1113    |0             |X1110
  X1114    |0             |X1110
  </code></pre>
  
  <p>What I want to acheive is to have this kind of sequence number:</p>
  
  <pre><code>Seq |Code     |GroupLevel    |Group
  -------------------------------------
  1   |X0000    |4             |
  2   |X1000    |3             |X0000
  3   |X1100    |2             |X1000
  4   |X1110    |1             |X1100
  5   |X1111    |0             |X1110
  6   |X1112    |0             |X1110
  7   |X1113    |0             |X1110
  8   |X1114    |0             |X1110
  9   |X1120    |1             |X1100
  10  |X1200    |2             |X1000
  11  |X1300    |2             |X1000
  12  |X2000    |3             |X0000
  13  |X2100    |2             |X2000
  14  |X2200    |2             |X2000
  15  |X2300    |2             |X2000
  16  |X3000    |3             |X0000
  </code></pre>
  
  <p>I tried something with fixed group level (3) but not if the group level > 5.</p>
  
  <p>Here's what I did:</p>
  
  <pre><code>List&lt;MySequenceModel&gt; _lstPair = new List&lt;MySequenceModel&gt;();
  var _lst = _records.Where(x =&gt; x.GroupLevel == 3).ToList();
  foreach (var item in _lst)
  {
      if (_lstPair.Where(x =&gt; x.Code.Equals(item.Code)).FirstOrDefault() == null)
          _lstPair.Add(new MySequenceModel { Code = item.Code, SeqNo = _seqCounter++ });
  
      var _lst2 = _records.Where(x =&gt; x.Group.Equals(item.Code) &amp;&amp; !x.Code.Equals(item.Code)).OrderBy(x =&gt; x.Code).ToList();
      foreach (var _item2 in _lst2)
      {
          if (_lstPair.Where(x =&gt; x.Code.Equals(_item2.Code)).FirstOrDefault() == null)
              _lstPair.Add(new MySequenceModel { Code = _item2.Code, SeqNo = _seqCounter++ });
          var _lst3 = _records.Where(x =&gt; x.Group.Equals(_item2.Code) &amp;&amp; !x.Code.Equals(_item2.Code)).OrderBy(x =&gt; x.Code).ToList();
          foreach (var _item3 in _lst3)
          {
              if (_lstPair.Where(x =&gt; x.Code.Equals(_item3.Code)).FirstOrDefault() == null)
                  _lstPair.Add(new MySequenceModel { Code = _item3.Code, SeqNo = _seqCounter++ });
              var _lst4 = _records.Where(x =&gt; x.Group.Equals(_item3.Code) &amp;&amp; !x.Code.Equals(_item3.Code)).OrderBy(x =&gt; x.Code).ToList();
              foreach (var _item4 in _lst4)
              {
                  if (_lstPair.Where(x =&gt; x.Code.Equals(_item4.Code)).FirstOrDefault() == null)
                      _lstPair.Add(new MySequenceModel { Code = _item4.Code, SeqNo = _seqCounter++ });
              }
          }
      }
  }
  </code></pre>
  
  <p><code>MySequenceModel</code> is a class that has the code and the sequence number which is essentially needed for reporting. A pseudo code will do.</p>
  
  <p>TIA</p>