我有一个修复字典键X
,我想List<Student>
用这个键创建一个,X
就像finalResult
我data
从一些外部来源获得的一样。
在下面的代码中,我收到错误,An item with the same key has already been added. Key: X'
. 如何解决?
const string dictKey = "X";
var finalResult = new Dictionary<string, List<Student>>();
var data = new Dictionary<string, int> {{"A1!D1", 10}, {"A2!D2", 20}};
foreach (var (key, value) in data)
{
finalResult.Add(dictKey, new List<Student>
{
new Student
{
Name = key.Split('!')[0],
Section = key.Split('!')[1],
Age = value
}
});
}