2014-12-01 83 views
3

我想添加一个项目到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; 
    } 
+0

'它不喜欢list.Add(newItem).'你是什么意思? – 2014-12-01 19:01:49

+1

intelisense说IEnumerable SelectListItem不包含添加的定义 – 2014-12-01 19:03:57

回答

8

的问题是,你的变量是IEnumerable<SelectListItem>类型。将其更改为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; 
    } 
+0

谢谢Vsevolod,这个技巧。 – 2014-12-01 19:18:58

0

这是我想出的东西,可能有助于某人。甚至可能在晚些时候。看看最后一行代码。

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