Razort4x
Razort4x

Reputation: 3406

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

Okay, so you got the idea from title, let me just post the code and in comments I'll explain what's happening (and what should!)

// 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);
            }

I Guess I explained everything in the code comments, but if you still need clarification on anything, feel free to ask. But basically, like I said, when I get to SomeMethod, the List doesn't seems to maintain its items (if more then 11) (is this odd or what?) So, Can anyone please help me with this? Any help would be appreciated.

UPDATE: Its not that 11 items are getting stored, its that whatever is the number of classes for last subject, that number of items are getting stored. Say if in upper loop, last id of subject is 52, and for that id there are 23 classes, then for every record inserted, even before, the _clsSub value's list will have at most 23 items.

Upvotes: 0

Views: 132

Answers (1)

yamen
yamen

Reputation: 15618

You declare this once:

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

Since it's a reference type, whenever you're using it, you're actually using a reference to it. So when you do this in your loop:

_clsSub.Add(nTempID, lstTempSub);

You're adding the same list to every slot in the dictionary. And when you do this:

lstTempSub.Clear();

You're clearing that same list in every slot in the dictionary. As such, all the lists in your dictionary are the same list, and end up containing what was in the last loop (that is, the last time you cleared it and added to it).

You need to move List<Int32> lstTempSub = new List<int>(); into the while loop in place of the line lstTempSub.Clear(); and everything will work.

(In answer to your title question in case it wasn't obvious - the last thing you loop through has 11 items, so all of them seem to have 11 items because they point to that same one)

Upvotes: 5

Related Questions