Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a resource set into dictionary using linq

Tags:

c#

linq

var rm = new ResourceManager(sometype);

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

I want to convert the above resource set into dictionary. Currently I'm doing through manually by looping as 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 I can do achieve that easily using linq?

like image 481
VJAI Avatar asked Jun 08 '12 12:06

VJAI


1 Answers

Try this:

var resourceDictionary = resourceSet.Cast<DictionaryEntry>()
                                    .ToDictionary(r => r.Key.ToString(),
                                                  r => r.Value.ToString());
like image 192
dtb Avatar answered Oct 05 '22 21:10

dtb