我正在尝试向IEnumerable SelectList中添加一个项目。 我有一个初始查询来填充我的列表,然后我有一个查询来检查是否存在名为“INFORMATIONAL”的项目。 如果没有,我需要将其添加到从我的初始查询返回的列表中。 这是我的代码。 它不喜欢list.Add(newItem)。 任何帮助将不胜感激。 谢谢
我试图向IEnumerable SelectList中添加一个项。我有一个初始查询来填充我的列表,然后我有一个查询来检查是否存在名为“INFORMATIONAL”的项。如果不存在,我需要将其添加到我的初始查询返回的列表中。以下是我的代码。它不喜欢list.Add(newItem)。任何帮助将不胜感激。谢谢。
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;
}