Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get keys and values from Appsettings of Config file in Linq

Tags:

c#

linq

I am trying to get all the keys and values of config file's app settings in a page constructor so I can use them in the page. I tried Linq but I am not sure how to get values along with keys in a simple manner. right now I got all keys and then using foreach to get all values and I am sure that is not a smart way. Please advice..

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

Thanks

like image 513
challengeAccepted Avatar asked Jan 06 '23 23:01

challengeAccepted


1 Answers

It sounds like you actually want 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]);
like image 195
David L Avatar answered Jan 16 '23 12:01

David L