我有一个类型字典Dictionary<string, IEnumerable<string>>
和一个字符串值列表.出于某种原因,每次执行Add时,都会覆盖字典中的每个值.我完全不知道为什么会这样.我确定在循环中声明和初始化IEnumberable对象不是引用问题,因此它的范围不会超出一次迭代,它仍然会这样做.这是我的代码:
foreach (string type in typelist)
{
IEnumerable<string> lst =
from row in root.Descendants()
where row.Attribute("serial").Value.Substring(0, 3).Equals(type)
select row.Attribute("serial").Value.Substring(3).ToLower();
serialLists.Add(type, lst);
}
Run Code Online (Sandbox Code Playgroud)
在哪里typelist
是一个IEnumerable<string>
,root
是一个XElement
,serialLists
是我的词典.