[英]How to add item to IEnumerable SelectListItem
I am trying to add an item to an IEnumerable SelectList. 我正在尝试将项添加到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.
我有一个初始查询填充我的列表,然后我有一个查询来检查是否存在名为“INFORMATIONAL”的项目。 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).
它不喜欢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>
. 问题是您的变量是
IEnumerable<SelectListItem>
类型。 Either change it to List<SelectListItem>
or use another variable. 将其更改为
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;
}
Essentially, you cannot, because IEnumerable does not necessarily represent a collection to which items can be added. 基本上,你不能,因为IEnumerable不一定代表可以添加项目的集合。 See this question on SO.
在SO上看到这个问题。
How can I add an item to a IEnumerable<T> collection? 如何将项添加到IEnumerable <T>集合中?
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());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.