C# 如何将项添加到IEnumerable SelectListItem
我正在尝试将项目添加到IEnumerable SelectList。我有一个初始查询来填充我的列表,然后我有一个查询来检查是否存在一个名为“信息”的项。如果没有,我需要将其添加到从初始查询返回的列表中。这是我的密码。它不喜欢list.Add(newItem)。任何协助都将不胜感激。谢谢C# 如何将项添加到IEnumerable SelectListItem,c#,asp.net-mvc-5,C#,Asp.net Mvc 5,我正在尝试将项目添加到IEnumerable SelectList。我有一个初始查询来填充我的列表,然后我有一个查询来检查是否存在一个名为“信息”的项。如果没有,我需要将其添加到从初始查询返回的列表中。这是我的密码。它不喜欢list.Add(newItem)。任何协助都将不胜感激。谢谢 public IEnumerable<SelectListItem> GetCategoriesByAccountID(string AccountID) { IEnumera
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;
}
public IEnumerable GetCategoriesByAccountID(字符串AccountID)
{
IEnumerable list=null;
使用(var context=new AMPEntities())
{
//按AccountID查询数据库中的类别列表
var query=(来自context.CustomAlerts中的ca)
其中ca.AccountID==AccountID
orderby ca.AlertCategory
选择新的SelectListItem{Text=ca.AlertCategory,Value=ca.AlertCategory}).Distinct();
list=query.ToList();
//检查列表以查看“信息”是否已存在
变量项=(从列表中的l开始)
其中l.Value==“信息性”
选择新的SelectListItem{Text=l.Text,Value=l.Value});
//如果“信息”不存在,则将其添加到列表中
如果(项==null)
{
var newItem=new SelectListItem{Text=“informatical”,Value=“informatical”};
列表。添加(新项);
}
}
退货清单;
}
本质上,您不能这样做,因为IEnumerable不一定表示可以添加项的集合。看看这个问题吧
问题在于变量的类型为
IEnumerable
。将其更改为列表
,或使用另一个变量
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;
}
public IEnumerable GetCategoriesByAccountID(字符串AccountID)
{
List=null;
使用(var context=new AMPEntities())
{
//按AccountID查询数据库中的类别列表
var query=(来自context.CustomAlerts中的ca)
其中ca.AccountID==AccountID
orderby ca.AlertCategory
选择新的SelectListItem{Text=ca.AlertCategory,Value=ca.AlertCategory}).Distinct();
list=query.ToList();
//检查列表以查看“信息”是否已存在
变量项=(从列表中的l开始)
其中l.Value==“信息性”
选择新的SelectListItem{Text=l.Text,Value=l.Value});
//如果“信息”不存在,则将其添加到列表中
如果(项==null)
{
var newItem=new SelectListItem{Text=“informatical”,Value=“informatical”};
列表。添加(新项);
}
}
退货清单;
}
我想到了一些可能对某人有帮助的东西。也许我以后也会这样。请看最后一行代码
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());
}
CostCenterHeaders CostHeaders=CostCenterHeaders.GetCostCenterHeaders(ClientNumber);
List Level1Header=新列表();
如果(CostHeaders.Level1Heading!=null)
{
Level1Header.Add(新的SelectListItem{Text=“All”+CostHeaders.Level1Heading+“Centers”,Value=”“});
List HierarchyLevels=HierarchyLevel.GetHierarchyByLevel(ClientNumber);
Level1Header.AddRange(HierarchyLevels.Select(x=>newselectListItem(){Value=x.LevelID,Text=x.LevelDescr}).ToList());
}
它不喜欢list.Add(newItem)。
这是什么意思?Intelissense说IEnumerable SelectListItem不包含Add的定义