How to get keys and values ​​from Config application settings in Linq

I am trying to get all the keys and parameter values ​​of a configuration file application in the page constructor so that I can use them on the page. I tried Linq, but I'm not sure how to get the values ​​along with the keys in a simple way. right now i got all the keys and then used foreach to get all the values ​​and i'm sure this is not a smart way. Please advice.

 string[] repositoryUrls = ConfigurationManager.AppSettings.AllKeys                            
                         .Select(key => ConfigurationManager.AppSettings[key])                                               
                         .ToArray();

thanks

+4
source share
1 answer

It looks like you really want to use a dictionary of key names and value values ​​instead of a string array.

var dict = 
    ConfigurationManager.AppSettings.AllKeys
        .ToDictionary(k => k, v => ConfigurationManager.AppSettings[v]);
+8
source

Source: https://habr.com/ru/post/1621149/


All Articles