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, after which I have a query to check if an item named INFORMATION exists. If not, I need to add it to the list returned from my original request. Here is my code. He doesn't like list.Add (newItem). Any help would be appreciated. Thanks to

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

      

+3


source to share


3 answers


The problem is that your variable is of type IEnumerable<SelectListItem>

. Either change it to List<SelectListItem>

or use a different 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;
    }

      

+8


source


Essentially you cannot, because IEnumerable does not necessarily represent a collection to which elements can be added. See this question on SO.



How do I add an element to an IEnumerable <T> collection?

0


source


Here is what I came up with that might help someone. Perhaps even I am 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());
        }

      

0


source







All Articles