2012-06-08 9 views
8
var rm = new ResourceManager(sometype); 

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

Quiero convertir el conjunto de recursos anterior en diccionario. Actualmente lo estoy haciendo de forma manual mediante un bucle como el siguiente.Convertir un conjunto de recursos en diccionario utilizando linq

var resourceDictionary = new Dictionary<string, string>(); 

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

¿Cómo puedo lograrlo fácilmente usando linq?

Respuesta

29

Prueba esto:

var resourceDictionary = resourceSet.Cast<DictionaryEntry>() 
            .ToDictionary(r => r.Key.ToString(), 
                r => r.Value.ToString()); 
+1

se olvidó por completo de este método 'Cast <>()! – superjos

2
var resourceDictionary = resourceSet.Select(r => (DictionaryEntry) r) 
            .ToDictionary(dicEntry => dicEntry.Key.ToString(), 
               dicEntry => dicEntry.Value.ToString()); 
Cuestiones relacionadas