user7255640
user7255640

Reputation:

Get ConfigurationManager.AppSettings Key name along with values

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

Here I'm getting all the AppSettings that starts with Account but currently they only return the value of the object.

Is there a way I can turn string[] Accounts into List<Tuple<String, String>> Accounts so it would be like List<Tuple<key, value>> Accounts

Upvotes: 2

Views: 378

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

Absolutely - all you need to do is changing your Select to return the desired tuple, and calling ToList in place of ToArray:

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

Upvotes: 1

Related Questions