我想在 MVC 中创建一个包含特定数字的下拉列表
所以在我的模型中我有一个属性:
public IEnumerable<SelectListItem> Quantities { get; set; }
在我的 Controller 中,我实例化了我的模型:
Quantities = from s in numbers
select new SelectListItem { Text = s.ToString()}
numbers 是一个 int 数组,里面有 10 个数字。
这段代码可以工作,但问题是我无法从 's' 获取数组的值(索引)。
有谁知道我如何获得这个值?
最佳答案
这对于 LINQ 表达式来说是不可能的。您可以使用以下内容:
Quantities = numbers.Select((s, index) => new SelectListItem
{
Value = index.ToString(),
Text = s.ToString()
);
关于c# - 使用linq查询时如何获取int数组的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9149944/