I have created a Dictionary called 'sGC' that has a string key and a value of a Tuple containing 2 lists of strings.
Dictionary<string, Tuple<List<string>, List<string>>> sGC = new Dictionary<string, Tuple<List<string>, List<string>>>();
I want to add new keys to this dictionary that are concatenated strings from a DataTable DataRow (DR). If a certain criteria is met then a string from the DR goes in either Item1 or Item2 of the Tuple.
This code is being executed in a foreach loop iterating through the DataTable, stopping on certain rows if the row meets an if statement criteria.
var dicTup = new Tuple<List<string>,List<string>>(new List<string>(), new List<string>());
dicTup.Item2.Add(DR["PupilID"].ToString());
sGC.Add(DR["CSN"].ToString() + DR["AW2"].ToString(), dicTup);
Is this the best way to add new dynamically named keys to the dictionary?
I believe the top answer from this JavaScript thread is an answer that I am looking for in C#: How to create dictionary and add key–value pairs dynamically?
Full code below.
foreach (DataRow DR in MainData.DataTable.Rows)
{
//Rows containing a symbol mark score
if ((DR["CN"].ToString() == "LC") && (DR["AW2"].ToString() != ""))
{
//Store male results
//If the Subject Name + Level Code is already a key in the dictionary, append to Tuple List 1
//If key does not exist in Dictionary, create new DictKey and value
if (DR["PG"].ToString() == "Male")
{
if (sGC.ContainsKey(DR["CSN"].ToString() + DR["AW2"].ToString()))
{
sGC[DR["CSN"].ToString() + DR["AW2"].ToString()].Item1.Add(DR["PID"].ToString());
}
else
{
var dicTup = new Tuple<List<string>,List<string>>(new List<string>(), new List<string>());
dicTup.Item1.Add(DR["PID"].ToString());
sGC.Add(DR["CSN"].ToString() + DR["AW2"].ToString(), dicTup);
}
}
//Store female results
//If the Subject Name + Level Code is already a key in the dictionary, append to Tuple List 2
//If key does not exist in Dictionary, create new DictKey and value
if (DR["PG"].ToString() == "Female")
{
if (sGC.ContainsKey(DR["CSN"].ToString() + DR["AW2"].ToString()))
{
sGC[DR["CSN"].ToString() + DR["AW2"].ToString()].Item2.Add(DR["PID"].ToString());
}
else
{
var dicTup = new Tuple<List<string>,List<string>>(new List<string>(), new List<string>());
dicTup.Item2.Add(DR["PupilID"].ToString());
sGC.Add(DR["CSN"].ToString() + DR["AW2"].ToString(), dicTup);
}
}
}
Newly edited and formatted code:
private void storeMarkSheetData()
{
if (MainData.DataTable != null)
{
if(subjectGradeCounts.Count == 0)
{
foreach (DataRow DR in MainData.DataTable.Rows)
{
string cN = DR["ColumnName"].ToString();
string aW2 = DR["AssessmentAwarded2"].ToString();
string cSN = DR["ClassSubjectName"].ToString();
string pID = DR["PupilID"].ToString();
string pG = DR["PupilGender"].ToString();
//Rows containing a symbol mark score
if ((cN == "Level Code") && (aW2 != ""))
{
//Check to see if the dictionary contains the key, adds it if not
if(!subjectGradeCounts.ContainsKey(cSN + aW2))
{
subjectGradeCounts.Add(cSN+aW2, new
Tuple<List<string>, List<string>>(new List<string>(), new
List<string>()));
}
//Now that the key exists, if it didn't previously
//If male add to list 1, else list 2 (for female)
if(pG == "Male")
{
subjectGradeCounts[cSN + aW2].Item1.Add(pID);
}
else
{
subjectGradeCounts[cSN + aW2].Item2.Add(pID);
}
}
}
}
}
}
Thank you all.