簡體   English   中英

列表中不能存儲11個以上的項目 <Int32> 在字典中 <int, List<Int32> &gt;

[英]Cannot store more than 11 items in a List<Int32> in a Dictionary<int, List<Int32>>

好的,所以您從標題中了解了這個主意,讓我發布代碼,並在注釋中解釋發生了什么(應該發生什么!)。

// select distinct subject from database
cmd.CommandText = "SELECT DISTINCT SubjectId  FROM ClassSubject";
// a new command
OleDbCommand cmdTemp = new OleDbCommand();
// con id defined earlier
cmdTemp.Connection = con;
// Reader for outer loop
OleDbDataReader rdr;
// reader for inner loop
OleDbDataReader rdrTemp;
// reader for main command, that is for each subject
rdr = cmd.ExecuteReader();
// this will store subject id
int nTempID;
// this is the list that is supposed to contain all the classes (many to many relation btw class and subject)
// it does gets set fine, but later only 11 items show up
List<Int32> lstTempSub = new List<int>();
// a dictionary that will store all classes for this each particular subject
_clsSub = new Dictionary<int, List<int>>();
// read main, i.e. read subjects
while (rdr.Read())
{
    // set the id of subject
    nTempID = rdr.GetInt32(0);
    // clear previous items
    lstTempSub.Clear();
    // this selects all the classes for this particular subject
    cmdTemp.CommandText = "SELECT ClassId FROM ClassSubject WHERE SubjectID=" + nTempID + " ORDER BY ClassID";
    // Execute in the tempReader
    rdrTemp = cmdTemp.ExecuteReader();
    // read
    while (rdrTemp.Read())
    {
        // here, we add all the classes that are there for this particular subject to the list we createed
        lstTempSub.Add(rdrTemp.GetInt32(0));
    }
    // close inner one
    rdrTemp.Close();
    // every thing is fine till here,
    // i.e. nTempId is what is should be, the id of this particular subect
    // lstTempSub contains all class for this subject, may be 1 or may be 100
    _clsSub.Add(nTempID, lstTempSub);
}
// close outer one
rdr.Close();

// Here is where things get wrong
 foreach(KeyValuePair<Int32, List<Int32>> pair in _clsSub)
            {
                // when the debugger gets here, the key is fine for each subject
                // but pair.Value always have 11 values at most, if for a subject
                // there were less class, then it shows okay,
                // but in case there were more than 11, only 11 values are shown in pair.value
                SomeMethod(pair.Key, pair.Value);
            }

我想我已經解釋了代碼注釋中的所有內容,但是如果您仍然需要澄清任何內容,請隨時提出。 但是基本上,就像我說的那樣,當我進入SomeMethod時,列表似乎並沒有維護它的項(如果大於11)(這是奇怪的還是什么?)所以,有人可以幫我嗎? 任何幫助,將不勝感激。

更新: 不是要存儲11個項目,而是最后一個主題的類數是多少,即要存儲的項目數。 假設如果在上層循環中,subject的最后一個id是52,並且該id有23個類,那么對於插入的每條記錄,即使在以前, _clsSub值的列表最多也有23個項目。

您只需聲明一次:

List<Int32> lstTempSub = new List<int>();

由於它是引用類型,因此無論何時使用它,實際上都在使用對它的引用。 因此,當您在循環中執行此操作時:

_clsSub.Add(nTempID, lstTempSub);

您要將相同的列表添加到字典中的每個插槽。 當您這樣做時:

lstTempSub.Clear();

您正在清除字典中每個插槽中的相同列表。 這樣,字典中的所有列表都是相同的列表,並且最終包含上一個循環(即您上次清除並添加到該循環中的內容)中的內容。

您需要移動List<Int32> lstTempSub = new List<int>(); 放入while循環中,代替lstTempSub.Clear();lstTempSub.Clear(); 一切都會正常。

(在標題不明確的情況下回答您的標題問題-您循環通過的最后一件事有11個項目,因此它們似乎都包含11個項目,因為它們指向同一項目)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM