开发者

How to insert new item into an IEnumerable

I create a dropdownlist from Enum.

public enum Level
{
    Beginner = 1,
    Intermediate = 2,
    Expert = 3
}

here's my extension.

    public static SelectList ToSelectList<开发者_如何学C;TEnum>(this TEnum enumObj)
    {

        IEnumerable<TEnum> values = Enum.GetValues(typeof(TEnum)).Cast<TEnum>();

        var result = from TEnum e in values
                     select new { ID = (int)Enum.Parse(typeof(TEnum), e.ToString()), Name = e.ToString() };

        var tempValue = new { ID = 0, Name = "-- Select --" };


        return new SelectList(result, "Id", "Name", enumObj);
    }

the problem I have is to insert antoher item into IEnumerable. I just could not figure out how to do it. Can someone please modify my code to insert "--select--" to the top.


You can't modify a IEnumerable<T> object, it only provides an interface to enumerate elements. But you could use .ToList() to convert the IEnumerable<T> to a List<T>.

I'm not sure if this is what you want:

public static SelectList ToSelectList<TEnum>(this TEnum enumObj)
{

    IEnumerable<TEnum> values = Enum.GetValues(typeof(TEnum)).Cast<TEnum>();

    var result = from TEnum e in values
                 select new { ID = (int)Enum.Parse(typeof(TEnum), e.ToString()), Name = e.ToString() };

    var tempValue = new { ID = 0, Name = "-- Select --" };

    var list = result.ToList(); // Create mutable list

    list.Insert(0, tempValue); // Add at beginning of list

    return new SelectList(list, "Id", "Name", enumObj); 
}


You cannot modify an IEnumerable. As it name suggests it allows a forward-only enumeration traversal.

This being said it seems that this is an ASP.NET MVC application. The correct way to achieve what you are trying to achieve (insert a default value) for a dropdown is to use the proper overload of the DropDownFor helper, like this:

@Html.DropDownListFor(
    x => x.SomeValue, 
    Model.SomeEnum.ToSelectList(), 
    "-- Select --"
)

This obviously assumes that your extension method is as simple as:

public static SelectList ToSelectList<TEnum>(this TEnum enumObj)
{
    var result = 
        from e in Enum.GetValues(typeof(TEnum)).Cast<TEnum>()
        select new 
        { 
            Id = (int)Enum.Parse(typeof(TEnum), e.ToString()), 
            Name = e.ToString() 
        };
    return new SelectList(result, "Id", "Name", enumObj);
}


Give this a try

public static SelectList ToSelectList<TEnum>( this TEnum enumObj )
  {
     var result = ( from TEnum e in Enum.GetValues( typeof( TEnum ) )
                    select new
                    {
                       ID = (int) Enum.Parse( typeof( TEnum ), e.ToString() ),
                       Name = e.ToString()
                    } ).ToList();

     result.Insert( 0, new
                       {
                         ID = 0,
                         Name = "-- Select --"
                        } );

     return new SelectList( result, "Id", "Name", enumObj );
  }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜