使用List <string> data </string>填充IEnumerable集合

时间:2013-12-04 09:28:22

标签: c# list ienumerable

我有一个包含数据的现有List

List<string>countries

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

IEnumerable<SelectListItem> Countries

我将如何做到这一点?

3 个答案:

答案 0 :(得分:3)

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

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

但是,您无法将文本与值区分开来。

答案 1 :(得分:2)

假设这与mvc有关...试试这个

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

答案 2 :(得分:1)

我想是这样的。

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