我想添加一個項目到IEnumerable SelectList。我有一個初始查詢來填充我的列表,然後我有一個查詢來檢查是否存在名爲「INFORMATIONAL」的項目。如果不是,我需要將它添加到從我的初始查詢返回的列表中。這是我的代碼。它不喜歡list.Add(newItem)。任何援助將不勝感激。由於如何添加項目到IEnumerable SelectListItem
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;
}
'它不喜歡list.Add(newItem).'你是什麼意思? – 2014-12-01 19:01:49
intelisense說IEnumerable SelectListItem不包含添加的定義 – 2014-12-01 19:03:57