I have a fix dictionary key X
and I want to create a List<Student>
with this key X
as finalResult
where I have data
from some external source.
In below code I am getting error, An item with the same key has already been added. Key: X'
. How to fix it?
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
}
});
}