user3250818
user3250818

Reputation: 247

how to foreach enum from except first item

this is enum

public enum SportTypeEnum
{
    [EnumDescription("not defined")]
    Null = 0,

    [EnumDescription("football")]
    FootBall = 100,

    [EnumDescription("volyball")]
    VollyBall = 110,

    [EnumDescription("basketball")]
    BasketBall = 120,

    [EnumDescription("Swimming")]
    wrestling = 140,
}

I can loop through all element like below

var sportTypeValueList = Enum.GetValues(typeof(SportTypeEnum));
@foreach (SportTypeEnum sportTypeEnum in sportTypeValueList)
{                   
    <option value="@(sportTypeEnum.ToString())" @(dataUi != null && dataUi.SportType == sportTypeEnum ? "selected=\"selected\"" : "")>
        @EnumUtilities.GetEnumDescription(sportTypeEnum)
    </option>
}

but how can loop through item except first item? thanks

Upvotes: 2

Views: 1623

Answers (1)

mm8
mm8

Reputation: 169200

You could simply use an if statement to exclude the None option rather than excluding the "first" option:

var sportTypeValueList = Enum.GetValues(typeof(SportTypeEnum));
@foreach(SportTypeEnum sportTypeEnum in sportTypeValueList)
{
    @if(sportTypeEnum != SportTypeEnum.Null)
    {
        <option value = "@(sportTypeEnum.ToString())" @(dataUi != null && dataUi.SportType == sportTypeEnum ? "selected=\"selected\"" : "") >
           @EnumUtilities.GetEnumDescription(sportTypeEnum)
        </option >
    }
}

Upvotes: 4

Related Questions