【发布时间】:2018-08-20 15:31:05
【问题描述】:
我创建了一个名为“sGC”的字典,它有一个字符串键和一个包含 2 个字符串列表的元组值。
Dictionary<string, Tuple<List<string>, List<string>>> sGC = new Dictionary<string, Tuple<List<string>, List<string>>>();
我想向此字典添加新键,这些键是来自 DataTable DataRow (DR) 的串联字符串。如果满足某个条件,则来自 DR 的字符串将进入元组的 Item1 或 Item2。
此代码在遍历 DataTable 的 foreach 循环中执行,如果该行满足 if 语句条件,则在某些行上停止。
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);
这是向字典添加新的动态命名键的最佳方式吗?
我相信这个 JavaScript 线程的最佳答案是我在 C# 中寻找的答案:How to create dictionary and add key–value pairs dynamically?
完整代码如下。
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);
}
}
}
新编辑和格式化的代码:
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);
}
}
}
}
}
}
谢谢大家。
【问题讨论】:
-
我要做的第一件事是检查字典是否已经包含您要添加的键,如果确实如此,您当前的代码将抛出异常,我将在下面添加我自己的示例尝试并防止异常。
-
嗨,Ryan,我已经完成了,我将代码添加到主体中,谢谢。
-
如果我必须为具有这种数据结构的项目做出贡献,我会问这是否真的有必要。似乎过于复杂。
-
我不是 100% 确定你想要完成什么,但是我之前尝试过使用元组字典,并且总是因为没有使用类而自责。
-
您还应该考虑将
Tuple替换为实际类。现在您正在创建一个Tuple并修改其中的值,但Tuple是不可变的,因此由于项目是List它可以工作,但如果您更改某些内容它将停止工作,这很令人困惑。