【发布时间】:2017-05-19 09:12:37
【问题描述】:
这些代码块是否相同?哪一个更好?我应该在 (2) 处初始化新字典吗?
XDocument docXml = XDocument.Load(filePath);
第一块:
var dictionary = new Dictionary<string, string>();
var temp = configXml.Root.Element("hs")
.Descendants("h")
.Select(x => new
{
a = x.Attribute("a").Value,
b = x.Value
});
foreach (var c in temp)
{
dictionary.Add(c.a, c.b);
}
第二块:
ConectionStrings = configXml.Root.Element("hs")
.Descendants("h")
.ToDictionary(x => x.Attribute("a").Value,
x => x.Value);
【问题讨论】:
-
什么是
list?代码块差别很大,应该如何比较呢? -
对不起,它只是从上层迭代。我忘记了第二个代码块的更改值。
标签: c# linq dictionary