0

I have a "foreach" in a table that adds multiple items to a list, so the added items don't get mixed up I added a "|" however I wanted this bar not to appear for the last item added.

How do I select the last item in the "foreach" and hide the bar?

-Image List:

enter image description here

<td style="text-align: center;">
   @if (string.IsNullOrWhiteSpace(Model.ListTipoPenal.SingleOrDefault(i => i.Value == item.COD_TIPO_PENAL_PRINCIPAL.ToString())?.Text))
   {
    <span> NCI - Não Consta Informação </span>
   }
   else
   {
      foreach (var tipoPenal in item.TipoPenalList)
      {
       var listaString = Model.ListTipoPenal.SingleOrDefault(i => i.Value == tipoPenal.ToString())?.Text.Split("-");
       var listaCodigo = listaString[listaString.Length - 1].Split(" ", StringSplitOptions.RemoveEmptyEntries);
       if (listaString.Length == 3)
       {
         <span>@listaString[0] - @listaString[1] - @listaCodigo[0] <strong> @listaCodigo[1] </strong></span>
       }
       else
       {
         <span>@listaString[0] - @listaCodigo[0] <strong> @listaCodigo[1] 
         <span style="margin-left: 0.3em;margin-right: 0.3em;">|</span></strong></span>
       }
      }
   }
</td>

1 Answers1

1

inside your foreach try the following...

foreach (var tipoPenal in item.TipoPenalList)
{
   if (tipoPenal == item.TipoPenalList[item.TipoPenalList.Count - 1])
   {
      //this is the last one so do something different
   }
   else
   {
      //this is the rest so do what you normally do
   }
}
mjb
  • 116
  • 3