title: How to compare current item with the next item in foreach asp.net
body: <p>I have a question.</p>
  
  <p>I have model, and I show in the page this model thanks to foreach. And in the model I have column (<code>vPeriod</code>) and I need to compare current item (e.g <code>vPeriod = 2016</code>) with next item (e.g. <code>vPeriod = 2017</code>) and I don't know how to compare!</p>
  
  <pre><code>foreach(var item in Model)
  {
  if(current.vPeriod == next.vPeriod)
  {
  //do something
  }
  else
  {
  //do something
  }
  }
  </code></pre>
  
  <p>If I use <code>for</code>, it's the easy way. But how do I do this with <code>foreach</code>?</p>
  
  <p>Code view:</p>
  
  <pre><code> &lt;tr class="line2"&gt;
                  &lt;td&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;/td&gt;
                  &lt;td style="color: #5f6877;"&gt;2016&lt;/td&gt;
                  &lt;td&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;/td&gt;
              &lt;/tr&gt;
  
  @foreach (var itemData in Model)
              {
                  if (itemData.vPeriod == "2016")
                  {
                      &lt;tr class="line3"&gt;
                          &lt;td&gt;@itemData.vAddress&lt;/td&gt;
                          &lt;td&gt;@itemData.vCreateDate&lt;/td&gt;
                          &lt;td&gt;@itemData.iCountOfFloors&lt;/td&gt;
                          &lt;td&gt;@itemData.iCountOfRooms&lt;/td&gt;
                          &lt;td&gt;@itemData.iAreaOfRooms&lt;/td&gt;
                          &lt;td&gt;@itemData.vLastRepairs&lt;/td&gt;
                          &lt;td&gt;@itemData.vType&lt;/td&gt;
                          &lt;td&gt;@itemData.vBelonging&lt;/td&gt;
                          &lt;td&gt;@itemData.vPlanRepairs&lt;/td&gt;
                      &lt;/tr&gt;
                  }
              }
  
              &lt;tr class="line2"&gt;
                  &lt;td&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;/td&gt;
                  &lt;td style="color: #5f6877;"&gt;2017&lt;/td&gt;
                  &lt;td&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;/td&gt;
              &lt;/tr&gt;
  
  
              @foreach (var itemData in Model)
              {
                  if (itemData.vPeriod == "2017")
                  {
                      &lt;tr class="line3"&gt;
                          &lt;td&gt;@itemData.vAddress&lt;/td&gt;
                          &lt;td&gt;@itemData.vCreateDate&lt;/td&gt;
                          &lt;td&gt;@itemData.iCountOfFloors&lt;/td&gt;
                          &lt;td&gt;@itemData.iCountOfRooms&lt;/td&gt;
                          &lt;td&gt;@itemData.iAreaOfRooms&lt;/td&gt;
                          &lt;td&gt;@itemData.vLastRepairs&lt;/td&gt;
                          &lt;td&gt;@itemData.vType&lt;/td&gt;
                          &lt;td&gt;@itemData.vBelonging&lt;/td&gt;
                          &lt;td&gt;@itemData.vPlanRepairs&lt;/td&gt;
                      &lt;/tr&gt;
                  }
              }
  </code></pre>
  
  <p>It's like really awful! I can have many different years and all the time need to create a block with data and a `foreach block for year.</p>