Get next record on a grouped foreach

Asked

Viewed 396 times

1

How could I be selecting the next record within a foreach grouped before it ends, without advancing on that loop?

The point where I think I might be getting this information is where it’s written GET NEXT FOREACH ID in the code below:

decimal? valor = 0;
@foreach (var item in Model.Lista.GroupBy(d => d.Nome)
                              .Select(
                               g => new
                               {
                                 key = g.Key,
                                 Id = g.First().Id,
                                 NomeFantasia = g.First().NomeFantasia,
                                 ValorTotal = g.Sum(s => s.ValorTotal) }))
{
   <tr>
     <td>Id</td>
     <td>Nome</td>
     <td>Valor</td>
   </tr>

   valor += item.Valor;

   <!- **OBTER O PRÓXIMO ID DESSE FOREACH** -->

}

   <tr>
     <td colspan="2"></td>
     <td>@Valor</td>
   </tr>
  • I don’t know if I understand what you want, but at the end of the ribbon will come the next id. Do you want the next one before you finish? Wouldn’t it be better to put the LINQ on controller, in view should have only code strictly necessary to generate the view, this LINQ code is system rules, or even I would say business rule, which should be in the model.

  • Exactly. I’d like to get the next one before I finish. Yes. agree and should LINQ be in the controller, but a problem arose and I had to implement so.

2 answers

1


You can’t use the for each when you want to control the index, so there is the for:

var valor = 0M;
@foreach (var i = 0; i < model.Count(); i++) {
    <tr>
        <td>Id</td>
        <td>Nome</td>
        <td>Valor</td>
    </tr>
    valor += model[i].Valor;
    var proximoID = model[i + 1].Id;
}

I put in the Github for future reference.

  • what type of model ? I set the model to List<>. but the length is not coming :

  • instead of using the model.length, I used the model. Count(); ai worked. Thank you very much.

  • It’s what you did I just avoided putting there, and I was wrong about it I’m gonna fix it :)

0

You can use a counter to be the iteration index and use the value i+1 to access your list.

@{int i = 0;}
@foreach(...)
{
    <span>@i+1</span>

    i++;
}

Edit:

var lista = Model.Lista.GroupBy(d => d.Nome)
                          .Select(
                           g => new
                           {
                             key = g.Key,
                             Id = g.First().Id,
                             NomeFantasia = g.First().NomeFantasia,
                             ValorTotal = g.Sum(s => s.ValorTotal) });

@{decimal? valor = 0;int i = 0;}
@foreach (var item in )
{
   <tr>
     <td>Id</td>
     <td>Nome</td>
     <td>Valor</td>
   </tr>

   valor += item.Valor;

   <!- **OBTER O PRÓXIMO ID DESSE FOREACH** -->
   lista[i++].Id
}
  • that’s not it. I’d like to get the next one before finishing the foreach

  • You have the current index, the proximum element will be the List[i+1] ... Being that Lista is the variable you will assign your GroupBy and after rforeach in that same variable. Unfortunate downvote.

Browser other questions tagged

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