简体   繁体   中英

How to add item to IEnumerable SelectListItem

I am trying to add an item to an 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. 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). 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> . Either change it to List<SelectListItem> or use another variable.

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. See this question on SO.

How can I add an item to a IEnumerable<T> collection?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM