Convert resource set to dictionary using linq

var rm = new ResourceManager(sometype); var resourceSet = rm.GetResourceSet(CultureInfo.CurrentUICulture, true, true); 

I want to convert the above resource set to a dictionary. I am currently doing this manually, looping as shown below.

 var resourceDictionary = new Dictionary<string, string>(); foreach (var r in resourceSet) { var dicEntry = (DictionaryEntry)r; resourceDictionary.Add(dicEntry.Key.ToString(), dicEntry.Value.ToString()); } 

How can I achieve this easily using linq?

+8
c # linq
source share
2 answers

Try the following:

 var resourceDictionary = resourceSet.Cast<DictionaryEntry>() .ToDictionary(r => r.Key.ToString(), r => r.Value.ToString()); 
+29
source share
 var resourceDictionary = resourceSet.Select(r => (DictionaryEntry) r) .ToDictionary(dicEntry => dicEntry.Key.ToString(), dicEntry => dicEntry.Value.ToString()); 
+2
source share

All Articles