我试图用where子句过滤SelectList,但是出现以下错误。
无法将类型为“ WhereEnumerableIterator`1 [System.Web.Mvc.SelectListItem]”的对象强制转换为“ System.Web.Mvc.SelectListItem”。
public SelectList ReadDocumentHeaderTypeList()
{
using (context = new Pinc_DBEntities())
{
List<SelectListItem> seList =
(from tb in context.tblDocumentHeaderTypes
select new SelectListItem
{
Value = SqlFunctions.StringConvert((double)tb.DocumentHeaderTypeID).Trim(),
Text = tb.DocumentHeaderTypeDescription,
}).OrderBy(o => o.Text).ToList();
SelectList slist = new SelectList(seList, "Value", "Text");
return slist;
}
}
SelectList sl = (SelectList)rep.ReadDocumentHeaderTypeList().Where(o => o.Value == "5" && o.Value == "6");//Error occurs here
您SelectList
似乎包含一个SelectListItem
对象列表。我不确定内部外观如何,但是您必须将项目列表提供给的新实例SelectList
。我认为您必须再次执行该操作。
... = new SelectList(rep.ReadDocumentHeaderTypeList()
.Where(o => o.Value == "5" || o.Value == "6").ToList(),
"Value", "Text");
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句