如何从 appSettings 获取键值对到 SelectListItem 的集合中?

How to get key value pair from appSettings into a collection of SelectListItem?

 <add key="Domain1" value="GREAT"/>
<add key="Domain2" value="NA"/>
<add key="Domain3" value="NZDOM"/>

   IEnumerable<SelectListItem> domainItems = ConfigurationManager.AppSettings.AllKeys
                             .Where(key => key.StartsWith("Domain"))
                             .Select(x => new SelectListItem
                             {
                                 Value = "???", //Want the key not a collection
                                 Text = "???" //Want the value not a collection
                             });

我想问题出在 Allkeys 对象上?所以 select 正在按照我的想象行事。

你的x是关键,所以你只需要使用x本身和Get来获取值:

{
    Value = x,
    Text = ConfigurationManager.AppSettings.Get(x)
}