获取ConfigurationManager.AppSettings键名以及值

时间:2018-03-18 13:04:20

标签: c# configurationmanager

string[] Accounts = ConfigurationManager.AppSettings.AllKeys
                        .Where(key => key.StartsWith("Account"))
                        .Select(key => ConfigurationManager.AppSettings[key])
                        .ToArray();

这里我得到的所有AppSettings都以Account开头,但目前只返回对象的值。

我有没有办法将string[] Accounts变成List<Tuple<String, String>> Accounts所以它就像List<Tuple<key, value>> Accounts

1 个答案:

答案 0 :(得分:1)

绝对 - 您需要做的就是更改Select以返回所需的元组,并调用ToList代替ToArray

List<Tuple<String,String>> Accounts = ConfigurationManager.AppSettings.AllKeys
    .Where(key => key.StartsWith("Account"))
    .Select(key => Tuple.Create(key, ConfigurationManager.AppSettings[key]))
    .ToList();
相关问题