title: C# apply sequence number to group items
body: <p>I am trying to group a collection of objects, sorting by Name, but I also want to set a sequential InstanceID property within each group, such that I have the following:</p>
  
  <pre><code>Name="A", InstanceID=0
  Name="A", InstanceID=1
  Name="A", InstanceID=2
  Name="B", InstanceID=0
  Name="C", InstanceID=0
  Name="C", InstanceID=1
  Name="C", InstanceID=2
  Name="D", InstanceID=0
  Name="D", InstanceID=1
  Name="D", InstanceID=2
  Name="D", InstanceID=3
  </code></pre>
  
  <p>etc.</p>
  
  <p>I'm assuming there is some projection applied via LINQ after <code>GroupBy</code> but I'm unaware of how to use an accumulator or index within the <code>Select</code> or <code>SelectMany</code>.</p>
  
  <p>I do not want to create a new collection, but only modify the existing collection, setting <code>InstanceID</code> on each element.</p>