3

我的 C# 代码有问题。我不断收到错误消息“已添加具有相同密钥的项目”,我尝试了网上建议的各种方法,但似乎无法摆脱它。有人可以帮忙吗。我在这条线上得到了错误

ExistIncInsList.Add(WSIncInstOutput[Insrow][0], Int32.Parse(WSIncInstOutput[Insrow][1]));

以下是涉及此的代码部分。

卡拉博

Dictionary<string, int> ExistIncInsList = new Dictionary<string, int>();
for (int Insrow = 2; Insrow < WSIncInstOutput.Count(); Insrow++)
{
    int existincWSInsID = Int32.Parse(WSIncInstOutput[Insrow][1]);
    if (!ExistIncInsList.ContainsKey(WSInsName))
    {
         ExistIncInsList.Add(WSIncInstOutput[Insrow][0], Int32.Parse(WSIncInstOutput[Insrow][1]));
    }
    if (MaxIncIndID < existincWSInsID)
    {
        MaxIncIndID = existincWSInsID;
        if (MaxIncIndID > MaxIndID)
        {
            MaxIndID = MaxIncIndID;
        } 
     }
}

if (ExistIncInsList.ContainsKey(WSInsName))
{
    WSInsID = ExistIncInsList[WSInsName];
}
else
{
    WSInsID = MaxIndID + 1;
    MaxIndID++;
}
4

1 回答 1

11

检查正确的密钥:

if (!ExistIncInsList.ContainsKey(WSIncInstOutput[Insrow][0]))

代替:

if (!ExistIncInsList.ContainsKey(WSInsName))
于 2013-01-30T15:01:24.620 回答