title: Avoid duplicates using foreach asp.net mvc 5
body: <p>I have created a tab where one adds their products and automatically it will add it on the tab. But now my issue is that it repeats the same thing on the tab view &quot;header&quot; whenever I have uploaded the same database. I want it to categorize it and not repeat it. Below is the code I have created.</p>
  <p>The picture below shows the database.
  <a href="https://i.stack.imgur.com/jBdsD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jBdsD.png" alt="enter image description here" /></a></p>
  <p>Controller</p>
  <pre><code>    public ActionResult Index()
      {
          return View(db.Products.ToList());
      }
  </code></pre>
  <p>View</p>
  <pre><code>    @model IEnumerable&lt;tkkkk.Models.Product&gt;
      @{
         ViewBag.Title = &quot;Home Page&quot;;
  
         int counterHeader = 0;
         int counterBody = 0;
       }
      &lt;br /&gt;
      &lt;ul class=&quot;nav nav-tabs&quot;&gt;
      @foreach (var product in Model)
      {
          
          if (counterHeader == 0)
          {
              &lt;li class=&quot;active&quot;&gt;&lt;a data-toggle=&quot;tab&quot; href=&quot;#@product.ProductId&quot;&gt;@product.ProductName&lt;/a&gt;&lt;/li&gt;
          }
  
          else
          {
              &lt;li&gt;&lt;a data-toggle=&quot;tab&quot; href=&quot;#@product.ProductId&quot;&gt;@product.ProductName&lt;/a&gt;&lt;/li&gt;
          }
          counterHeader++;
      }
     &lt;/ul&gt;
     &lt;br /&gt;
  
     &lt;div class=&quot;tab-content&quot;&gt;
     @foreach (var item in Model)
     {
      if (counterBody == 0)
      {
          &lt;div id=&quot;@item.ProductId&quot; class=&quot;tab-pane fade in active&quot;&gt;
              &lt;div class=&quot;col-lg-4 col-md-6 mb-4&quot; style=&quot;width:200px;&quot;&gt;
                  &lt;div class=&quot;card h-100&quot;&gt;
                      &lt;a href=&quot;#&quot;&gt;&lt;img class=&quot;card-img-top&quot; src=&quot;http://placehold.it/700x400&quot; alt=&quot;&quot; width=&quot;200&quot;&gt;&lt;/a&gt;
                      &lt;div class=&quot;card-body&quot;&gt;
                          &lt;h4 class=&quot;card-title&quot;&gt;
                              &lt;a href=&quot;#&quot;&gt;@item.ProductItem - @item.ProductName&lt;/a&gt;
                          &lt;/h4&gt;
                          &lt;h5&gt;R @item.ProductPrice&lt;/h5&gt;
                      &lt;/div&gt;
  
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      }
  
      else
      {
          &lt;div id=&quot;@item.ProductId&quot; class=&quot;tab-pane fade&quot;&gt;
              &lt;div class=&quot;col-lg-4 col-md-6 mb-4&quot; style=&quot;width:200px;&quot;&gt;
                  &lt;div class=&quot;card h-100&quot;&gt;
                      &lt;a href=&quot;#&quot;&gt;&lt;img class=&quot;card-img-top&quot; src=&quot;http://placehold.it/700x400&quot; alt=&quot;&quot; width=&quot;200&quot;&gt;&lt;/a&gt;
                      &lt;div class=&quot;card-body&quot;&gt;
                          &lt;h4 class=&quot;card-title&quot;&gt;
                              &lt;a href=&quot;#&quot;&gt;@item.ProductItem - @item.ProductName&lt;/a&gt;
                          &lt;/h4&gt;
                          &lt;h5&gt;R @item.ProductPrice&lt;/h5&gt;
                      &lt;/div&gt;
  
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;/div&gt;
              }
              counterBody++;
              }
          &lt;/div&gt;
  </code></pre>