使用 linq 将资源集转换为字典
本文关键字:转换 字典 资源 linq 使用 | 更新日期: 2023-09-27 18:36:38
var rm = new ResourceManager(sometype);
var resourceSet = rm.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
我想将上述资源集转换为字典。目前,我正在通过如下所示的循环手动完成。
var resourceDictionary = new Dictionary<string, string>();
foreach (var r in resourceSet)
{
var dicEntry = (DictionaryEntry)r;
resourceDictionary.Add(dicEntry.Key.ToString(), dicEntry.Value.ToString());
}
如何使用 linq 轻松实现这一点?
试试这个:
var resourceDictionary = resourceSet.Cast<DictionaryEntry>()
.ToDictionary(r => r.Key.ToString(),
r => r.Value.ToString());
var resourceDictionary = resourceSet.Select(r => (DictionaryEntry) r)
.ToDictionary(dicEntry => dicEntry.Key.ToString(),
dicEntry => dicEntry.Value.ToString());