【发布时间】:2014-05-07 18:23:15
【问题描述】:
我有一本字典,但在很多情况下可以向其中添加重复数据。我试图防止添加重复的键或值。但是,使用此代码,我仍然会收到“已添加具有相同密钥的项目”。如果尚未将新键和值添加到字典中,我只想将它们添加到字典中。
for (var i = 0; i < nodes.Count; ++i)
{
if (!webidsAndXPaths.ContainsKey(nodes[i].Id) || !webidsAndXPaths.ContainsValue(nodes[i].XPath)) // if the key or value does not exist then add it to the dictionary
webidsAndXPaths.Add(nodes[i].Id, nodes[i].XPath); // now we put the data in a dictionary <key = id, value = xpath>
}
【问题讨论】:
-
你确定你在这里使用了正确的数据结构吗?
-
不,我不是,哈哈。想要获取两条数据并将它们写入数据库。有时数据是多余的,所以如果它试图将它添加到字典中,那么它将无法工作。
标签: c#