0

当我加载页面时,我得到了一个具有相同键错误的项目。有什么解决方案可以解决这个问题吗?这是错误

一般信息

 - - - - - - - - - - - - - - - - - - - - - - - 

 MachineName: WIN-SKEC08HPAEB  FullName: InnoArk.APDMS.Common,
 Version=1.0.0.0, Culture=neutral, PublicKeyToken=null  AppDomainName:
 /LM/W3SVC/2/ROOT-1-130120289473554687  ThreadIdentity: innoark 

 1) Exception Information 
 - - - - - - - - - - - - - - - - - - - - - - -  Exception Type: System.ArgumentException  Message: An item with the same key has
 already been added.  ParamName: NULL  Data:
 System.Collections.ListDictionaryInternal  TargetSite: Void
 ThrowArgumentException(System.ExceptionResource)  HelpLink: NULL 
 Source: mscorlib 

 StackTrace Information 
 - - - - - - - - - - - - - - - - - - - - - - -  at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) 
 at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue
 value, Boolean add)  at
 System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value) 
 at InnoArk.APDMS.WebApp.Controllers.SimulationController._List(Guid
 GroupId, Guid bcTempId, String[] id, String[] volumeAnn, String[]
 volumeAvg, String[] costAvg, String[] costAnn, String[] costIfAvg,
 String[] costIfAnn, Boolean isEditBC, Boolean isBC, Boolean
 isSimulate, Boolean isSave) in
 D:\dev\APDMS\Trunk\InnoArk.APDMS.WebApp\Controllers\SimulationController.cs:line
 2478

抱歉,这是来自第 2478 行的 SimulationController 的代码:

detailList = new Dictionary<string, object>();
                    detailList.Add("id", entityId[j]);
                    detailList.Add("volAnn", volAnn[j]);
                    detailList.Add("volAvg", volAvg[j]);
                    detailList.Add("volLatestAnn", volLatestAnn[j]);
                    detailList.Add("volLatestAvg", volLatestAvg[j]);
                    detailList.Add("cAnn", cAnn[j]);
                    detailList.Add("cAvg", cAvg[j]);
                    detailList.Add("cIfAnn", cIfAnn[j]);
                    detailList.Add("cIfAvg", cIfAvg[j]);
                    detailList.Add("code", code[j]);
                    detailList.Add("isLatest", isLatest[j]);
                    detailList.Add("isDirty", isDirty[j]);
                    detailList.Add("curr", curr[j] == null ? "" : curr[j]);
                    detailList.Add("noOfOrder", noOfOrder[j]);
                    detailList.Add("isFilter", isFilter[j]);
                    tempList2.Add(entityId[j].ToString(), detailList); << line : 2478

感谢您尝试回答我的问题?:)

4

2 回答 2

3

这意味着名为的字典tempList2已经有一个值为 in 的键entityId[j]

看起来你的循环有问题,或者entityId包含重复的值。

假设这是第二个选项,请使用.Distinct()

entityId = entityId.Distinct().ToList();

如果entityId是普通数组而不是通用列表,请使用以下代码:

entityId = entityId.ToList().Distinct().ToArray();

无论如何,为了避免在这些情况下崩溃,您可以添加这样的验证:

if (tempList2.ContainsKey(entityId[j].ToString()))
{
    //already exists, you can show some alert here.
}
else
{
    tempList2.Add(entityId[j].ToString(), detailList);
}
于 2013-05-06T07:49:17.447 回答
2

看起来您正在将某些内容添加到通用字典中,并且那里不能有重复的键。

在添加之前验证它是否已经存在。

于 2013-05-03T05:44:29.553 回答