title: Displaying item other than key with groupby foreach loop c# asp.net mvc
body: <p>I am currently using groupby with a foreach loop to show a list of nested records.</p>
  
  <p>However I want to us an id as the key, so that I can use it to make dynamic css selectors etc..</p>
  
  <p>But I want to be able to show the name of each of the groups before the secondarly loop is iterated through.</p>
  
  <p>I think the code will make it more clear.</p>
  
  <p><strong>Basically it would be great if I could access properties other than group.key before the second loop starts.</strong></p>
  
  <p><strong>I know I could limit the iterations of the title loop, but it feels very wrong</strong></p>
  
  <pre><code>   &lt;ul id="GoalAndTaskList"&gt;
     &lt;% foreach (var group in Model.GroupBy(item =&gt; item.goalId)) { %&gt;
  
     &lt;li&gt;
  
     /////This code is just to display the name, but it iterates 
     /////through the whole group, so the title is rendered many times
  
     &lt;% foreach (var item in group)
     { %&gt;
     &lt;%= Html.Encode(item.goalName) %&gt;   
     &lt;% } %&gt;
  
     ///////// Before this I was using group.key , which meant an Id was the title
     /////////
  
     &lt;table id="taskTable&lt;%= Html.Encode(group.Key) %&gt;"&gt;
     &lt;% foreach (var item in group) { %&gt;
  
     &lt;tr id="&lt;%= Html.Encode(item.sortOrder) %&gt;"&gt;
     &lt;td class="dragHandle"&gt;&amp;nbsp;&lt;/td&gt;
     &lt;td&gt;&lt;%= Html.Encode(item.sortOrder) %&gt;&lt;/td&gt;
     &lt;td&gt;
     &lt;input type="text" name="&lt;%= Html.Encode(item.taskId) %&gt;" value="&lt;%=Html.Encode(item.taskName) %&gt;"/&gt;       
     &lt;/td&gt;
     &lt;td&gt;
     &lt;a id="DeleteTask&lt;%= Html.Encode(item.taskId) %&gt;"&gt;Delete Task&lt;/a&gt;
     &lt;/td&gt;       
     &lt;/tr&gt;  
     &lt;% } %&gt;    
     &lt;/table&gt;
     &lt;br /&gt;&lt;a id="AddNew2&lt;%= Html.Encode(group.Key) %&gt;"&gt;Add new task&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;    
     &lt;/li&gt;
     &lt;% } %&gt;
     &lt;/ul&gt;
  </code></pre>