如何将新项目插入 IEnumerable

发布于 2024-11-28 13:37:19 字数 711 浏览 1 评论 0原文

我从 Enum 创建一个下拉列表。

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

这是我的扩展。

    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 --" };


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

我遇到的问题是将另一个项目插入 IEnumerable 中。我只是不知道该怎么做。有人可以修改我的代码以在顶部插入“--select--”吗?

I create a dropdownlist from Enum.

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

here's my extension.

    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 --" };


        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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

残月升风 2024-12-05 13:37:19

您无法修改 IEnumerable 对象,它仅提供枚举元素的接口。但是您可以使用 .ToList()IEnumerable 转换为 List

我不确定这是否是您想要的:

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 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); 
}
百变从容 2024-12-05 13:37:19

您无法修改 IEnumerable。顾名思义,它允许仅向前的枚举遍历。

话虽如此,这似乎是一个 ASP.NET MVC 应用程序。实现下拉菜单想要实现的目标(插入默认值)的正确方法是使用 DropDownFor 帮助器的正确重载,如下所示:

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

这显然假设您的扩展方法非常简单:

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);
}

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);
}
长梦不多时 2024-12-05 13:37:19

尝试一下

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 );
  }

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 );
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文