Foreach without repeating the name

Asked

Viewed 244 times

1

I have a Foreach in View, and it contains Description, Qtd and dta,how do I make a foreach without repeating the description? Ex: I have a product X whose were removed 10 units day 01/10/2016, 3 units day 04/10/2016, 8 units day 15/10/2016, how do I display the description once and the dates and quantities normally?

<style type="text/css">
        .leftDivItem, .rightDivItem {
         border: none;
         float: left;
         width: 29%; /*Aumentar ou diminuir a porcentagem para adicionar ou tirar*/
         overflow: hidden;
         margin-right: 2%;
         margin-left: 2%;
         float: left;
         width: 19%;
         overflow: hidden;
         margin-right: 3%;
         margin-left: 3%;*/
  }
  </style>

   int i = 0;
   foreach (var item in Model)
       {
           if (i % 2 == 0)
           {
               <div class="leftDivItem">
                   <span class="listCol2">
                       <i>@Html.DisplayFor(modelItem => item.Produto.Descricao)</i><br />
                       <i>@Html.DisplayFor(modelItem => item.Produto.Qtd)</i><br />
                       <i>@Html.DisplayFor(modelItem => item.Produto.dta)</i>
                       <br />
                       <br />
                   </span>
               </div>
          }
          else
          {
              <div class="rightDivItem" id="textShadow">
                  <span class="listCol2">
                      <i>@Html.DisplayFor(modelItem => item.Produto.Descricao)</i><br />
                      <i>@Html.DisplayFor(modelItem => item.Produto.Qtd)</i><br />
                      <i>@Html.DisplayFor(modelItem => item.Produto.dta)</i>
                      <br />
                      <br />
                   </span>
              </div>
           }
           i++;
       }

in the code I’m showing, the

<i>@Html.DisplayFor(modelItem => item.Produto.Descricao)</i><br />

and I know he’ll repeat himself several times because he’s there, but I wanted to know how I position him not to repeat, and yes appear only once! Grateful!

1 answer

2


One way to solve the problem is by making a grouping by description. I will use the element table because the example becomes easier, you may have to make adjustments to your layout:

<table>
@foreach(var grupo in Model.GroupBy(i => i.Produto.Descricao)){
    <tr>
        <td rowSpan="@grupo.Count()"> grupo.Key </td> 
    <tr>
    @foreach(var item in grupo){
       <tr> 
           <td>item.Produto.Descricao</td>
           <td>item.Produto.Qtd</td>
           //...
        <tr>
    }
}
</table>

To order by quantity just put one OrderBy in the grupo.

grupo.OrderByDescending(i => i.Produto.Qtd)
  • It worked, but I was wondering how do I sort by the amount?

  • In case it would be so, it comes ordered by description, and each description has various quantities and dates, wanted in addition to sort by description, sort also by quantity. Ex: Product X - 10 units - 01/10/2016 3 units - 04/10/2016 8 units - 15/10/2016

  • Product Y - 2 units - 01/10/2016 30 units - 04/10/2016 8 units - 15/10/2016 were ordered by quantity... so: Product X - 10 units - 01/10/2016 8 units - 15/10/2016 3 tokens - 04/10/2016 Product Y - 30 tokens - 04/10/2016 8 tokens - 15/10/2016 2 tokens - 01/10/2016

  • @Fabiosouza I edited the answer

  • Thank you. saved my skin rs

  • @Fabiosouza If I helped, vote and accept the answer.

  • Sorry, I’m new here, I don’t understand much about how the site works... I accepted the answer, but how do I vote?

  • @Fabiosouza He has two arrows very close to accepting, one down (vote against) and the other up (positive vote). Take the tour

  • when I click to vote it appears this message

  • Thanks for the feedback! Votes cast by those with Less than 15 Reputation are Recorded, but do not change the publicly displayed post score

  • @Fabiosouza Sheltered by the warning Fabio. I will ask the community to translate the message. " Thanks for the feedback! Votes of users with less than 15 reputations are saved, but do not change the count of votes of post". I mean, don’t worry.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.