2013-05-03 88 views
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 

对不起,这里是从SimulationController的代码行2478:

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 

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

+0

“SimulationC”的第2478行ontroller.cs'看起来像?也许发布它和周围的线? – 2013-05-03 06:36:42

+0

你有没有调试过,你在'entityId [j] .ToString()' – Satpal 2013-05-06 07:52:01

+0

你得到了如何使用tempList2?这听起来像是存储在Tempdata中。 – 2013-05-06 08:34:49

回答

2

看起来你正在向通用字典中添加一些东西,并且你不能在那里有重复的键。

添加前确认它是否已经存在。

3

这意味着名为tempList2的字典已经有一个键值为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); 
} 
+0

谢谢。我遵循if条件,但仍然一样。我怎么把'entityId = entityId.Distinct()。ToList()?以前感谢:) – Anonymous 2013-05-06 09:37:00

+0

不,如果您有条件,您仍然无法获得相同的错误信息。你抄错了。至于这一行,只要把它放在循环的上面,列表就会变得清晰。 (没有重复的值,导致您正在获取的错误) – 2013-05-06 09:49:35

+0

我替换tempList2.Add(entityId [j] .ToString(),detailList);与if条件。我把代码放在错误的地方了吗?并为明显,它显示错误,其中包含“不能隐式转换类型'System.Collections.Generic.List ''string []'” – Anonymous 2013-05-06 09:58:06

相关问题