如何将项添加到 IEnumerable 选择列表项

本文关键字:IEnumerable 选择 列表 添加 | 更新日期: 2023-09-27 18:32:52

我正在尝试将一个项目添加到 IEnumerable SelectList 中。我有一个填充我的列表的初始查询,然后我有一个查询来检查是否存在名为"INFORMATIONAL"的项目。如果没有,我需要将其添加到初始查询返回的列表中。这是我的代码。它不喜欢列表。添加(新项)。任何协助将不胜感激。谢谢

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

如何将项添加到 IEnumerable 选择列表项

问题是您的变量类型为 IEnumerable<SelectListItem> 。将其更改为 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;
    }

本质上,您不能,因为 IEnumerable 不一定表示可以向其添加项的集合。请参阅SO上的此问题。

如何将项目添加到 IEnumerable 集合?

这是我想出的东西,可能对某人有所帮助。也许以后甚至我。看看最后一行代码。

        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());
        }
相关文章: