c# - 使用 List<string> 数据填充 IEnumerable 集合

标签 c# list ienumerable

<分区>

我有一个包含数据的现有列表

List<string>countries

我想以某种方式遍历这些数据并填充以下集合

IEnumerable<SelectListItem> Countries

我该怎么做?

最佳答案

我能想到的最简单的方法是使用 Linq 查询:

Countries = countries.Select(country => new SelectListItem {Text = country, Value = country});

但是,您无法区分文本和值。

关于c# - 使用 List<string> 数据填充 IEnumerable 集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20371520/

相关文章:

c# - .NET 如何确定字符的 Unicode 类别?

c# - 正则表达式中的 $1 和 $2 是什么?

java - 从列表中获取值并插入到字符串中

c# - 我可以将 C# 函数标记为 "this function does not enumerate the IEnumerable parameter"吗?

c# - IEnumerable 在数组和列表上的表现不同

c# - Window.Open 替换失败

Python 删除错误

c++ - 另一个 C++ 列表问题

c# - 产量继续?

c# - 使用迷你轮廓仪