title: Multiple foreach loops in razor
body: <p>I want to iterate through a collection using foreach, but I want properties of each object to determine where in the iteration it shows up. To fundamentally achieve this, I've used 2 foreach loops to first grab the primary list, then to grab the secondary list. The value is <strong>DEPtxt</strong> and the properties containing the value are <strong>Course_Departments</strong> and <strong>Course_Departments2</strong></p>
  
  <pre><code>@foreach (ContentNode node in pgnodes.Where(x =&gt; x.Data.Course_Departments.ToString() == DEPtxt)){
  
                  &lt;li class="sys_subitem" style="display: list-item;"&gt;&lt;a title="@node.Title" href="@node.Path"&gt;@node.Title&lt;/a&gt;&lt;/li&gt;  
              }
              @foreach (ContentNode node in pgnodes.Where(x =&gt; x.Data.Course_Departments2.ToString() == DEPtxt)){
  
                  &lt;li class="sys_subitem" style="display: list-item;"&gt;&lt;a title="@node.Title" href="@node.Path"&gt;@node.Title&lt;/a&gt;&lt;/li&gt;  
              }
  </code></pre>
  
  <p>To put into context the Linq query has grabbed all objects that have a string value in one of 2 properties of the objects. I want the foreach loop to show all objects that have it in <strong>Course_Departments</strong> first, then to <strong>Course_Departments2</strong> after that.</p>
  
  <p>Are multiple foreach loops the best way to do this?</p>
  
  <p>Do I have to sort at the query? Do I have to create a list that sorts it? I want to hit the data store as little as possible and have the razor engine do most of the work.</p>
  
  <p>Thanks!</p>