2013-05-03 43 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

相關問題