【发布时间】:2015-01-07 19:52:31
【问题描述】:
如何过滤从数据库中填充的 SelectList?在这个例子中,我只希望白色兔子(颜色是兔子对象的属性)出现在列表中。我试图在 Select 的末尾添加一个 where 但我只能看到 Id 和 Name 作为我可以过滤的条件。
var bunnies = db.Bunnies.Select(x => new SelectListItem
{
Value = x.Id.ToString(),
Text = x.Name,
}
);
return new SelectList(bunnies , "Value", "Text");
我想我可以这样做:
var bunnies = db.Bunnies.Select(x => new SelectListItem
{
Value = x.Id.ToString(),
Text = x.Name,
}
).Where(p => p.Color == "white");
return new SelectList(bunnies , "Value", "Text");
【问题讨论】:
标签: c# asp.net-mvc linq linq-to-sql