简体   繁体   English

如何将项添加到IEnumerable SelectListItem

[英]How to add item to IEnumerable SelectListItem

I am trying to add an item to an IEnumerable SelectList. 我正在尝试将项添加到IEnumerable SelectList。 I have an initial query that populates my list, I then have a query to check to see if an item called "INFORMATIONAL" exists. 我有一个初始查询填充我的列表,然后我有一个查询来检查是否存在名为“INFORMATIONAL”的项目。 If not, I need to add it to the list returned from my initial query. 如果没有,我需要将它添加到从我的初始查询返回的列表中。 Here is my code. 这是我的代码。 It does not like list.Add(newItem). 它不喜欢list.Add(newItem)。 Any assistance would be appreciated. 任何援助将不胜感激。 Thanks 谢谢

public IEnumerable<SelectListItem> GetCategoriesByAccountID(string AccountID)
    {
        IEnumerable<SelectListItem> list = null;

        using (var context = new AMPEntities())
        {
            // Queries DB for list of categories by AccountID
            var query = (from ca in context.CustomAlerts
                        where ca.AccountID == AccountID
                        orderby ca.AlertCategory
                        select new SelectListItem { Text = ca.AlertCategory, Value = ca.AlertCategory }).Distinct();
            list = query.ToList();

            // Checks list to see if "INFORMATIONAL" already exists
            var item = (from l in list
                        where l.Value == "INFORMATIONAL"
                        select new SelectListItem { Text = l.Text, Value = l.Value }).FirstOrDefault();

            // If "INFORMATIONAL" is not present add it to list
            if (item == null)
            {
                var newItem = new SelectListItem { Text = "INFORMATIONAL", Value = "INFORMATIONAL" };
                list.Add(newItem);
            }
        }

        return list;
    }

The problem is that your variable is of type IEnumerable<SelectListItem> . 问题是您的变量是IEnumerable<SelectListItem>类型。 Either change it to List<SelectListItem> or use another variable. 将其更改为List<SelectListItem>或使用其他变量。

public IEnumerable<SelectListItem> GetCategoriesByAccountID(string AccountID)
    {
        List<SelectListItem> list = null;

        using (var context = new AMPEntities())
        {
            // Queries DB for list of categories by AccountID
            var query = (from ca in context.CustomAlerts
                        where ca.AccountID == AccountID
                        orderby ca.AlertCategory
                        select new SelectListItem { Text = ca.AlertCategory, Value = ca.AlertCategory }).Distinct();
            list = query.ToList();

            // Checks list to see if "INFORMATIONAL" already exists
            var item = (from l in list
                        where l.Value == "INFORMATIONAL"
                        select new SelectListItem { Text = l.Text, Value = l.Value }).FirstOrDefault();

            // If "INFORMATIONAL" is not present add it to list
            if (item == null)
            {
                var newItem = new SelectListItem { Text = "INFORMATIONAL", Value = "INFORMATIONAL" };
                list.Add(newItem);
            }
        }

        return list;
    }

Essentially, you cannot, because IEnumerable does not necessarily represent a collection to which items can be added. 基本上,你不能,因为IEnumerable不一定代表可以添加项目的集合。 See this question on SO. 在SO上看到这个问题。

How can I add an item to a IEnumerable<T> collection? 如何将项添加到IEnumerable <T>集合中?

Here is something I came up with that might be helpful to someone. 这是我想出的一些可能对某人有帮助的东西。 Maybe even me at a later date. 甚至可能是我以后的日子。 Take a look at the last line of code. 看看最后一行代码。

        CostCenterHeaders CostHeaders = CostCenterHeaders.GetCostCenterHeaders(ClientNumber);
        List<SelectListItem> Level1Header = new List<SelectListItem>();
        if (CostHeaders.Level1Heading !=null)
        {
            Level1Header.Add(new SelectListItem { Text = "All " + CostHeaders.Level1Heading + " Centers", Value = "" });
            List<HierarchyLevel> HierarchyLevels = HierarchyLevel.GetHierarchyByLevel(ClientNumber);
            Level1Header.AddRange(HierarchyLevels.Select(x => new SelectListItem() { Value = x.LevelID, Text = x.LevelDescr }).ToList());
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用方法(IEnumerable <SelectListItem> ViewBag在数据库中显示保存的项目 - how to use (IEnumerable<SelectListItem>)ViewBag to show saved item in data base 如何将项目添加到 IEnumerable - How to add an item to an IEnumerable 没有类型为“ IEnumerable”的ViewData项 <SelectListItem> &#39;有钥匙 - There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 为什么会出现错误,“没有&#39;IEnumerable类型的ViewData项 <SelectListItem> “……” - Why is there an error, “There is no ViewData item of type 'IEnumerable<SelectListItem>'…” 没有带有IEnumerable类型的键&#39;taskTypes&#39;的ViewData项<SelectListItem> - There is no ViewData item with the key 'taskTypes' of type IEnumerable<SelectListItem> 没有类型为“ IEnumerable”的ViewData项 <SelectListItem> &#39;有钥匙&#39;...&#39; - There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key '…' 异常“没有类型为&#39;IEnumerable的ViewData项 <SelectListItem> ” MVC - Exception “There is no ViewData item of type 'IEnumerable<SelectListItem>” MVC 没有类型为“ IEnumerable”的ViewData项 <SelectListItem> &#39;有钥匙&#39;...&#39; - There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key '…' 如何在SelectListItem中删除项目 - How to remove an item in SelectListItem 如何没有&#39;IEnumerable类型的ViewData项 <SelectListItem> &#39;那里有&#39;Customer_id&#39;键 - How There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'Customer_id'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM