Udaya
Udaya

Reputation: 307

How to remove the first array element in a foreach loop?

How do I remove the first array item from holidayArray in the following code:

@foreach (var item in holidayArray)
{
   <p>@item</p>                
}

Upvotes: 0

Views: 389

Answers (2)

Hans Kesting
Hans Kesting

Reputation: 39274

Simply Skip that first item:

@foreach (var item in holidayArray.Skip(1))
{
     <p>@item</p>                
}

this will ignore 1 item from the start.

Upvotes: 3

User.Anonymous
User.Anonymous

Reputation: 1726

If it is for display in page, then you can ignore the first element by this way

@foreach (var (item, index) in holidayArray.Select((x,i) => (x, i)))
{
   if (index > 0) {
   @: <p>@item</p> 
   }               
}

Upvotes: 0

Related Questions