A Dictionary has two parts: a Key and a Value.
You code sets the Key only, so the Value is assumed to be the whole of the class instance that the initial collection had: a
Dictionary<string, studentData>>
rather than a
Dictionary<string, string>
.
To get a string Value you have to explicitly tell the ToDictionary method what value to use:
private void MyButton_Click(object sender, EventArgs e)
{
List<studentData> std = new List<studentData>{
new studentData { ID = "1", Name = "teststudent1" },
new studentData { ID = "2", Name = "teststudent2" },
};
Dictionary<string, string> dict = std.ToDictionary(k => k.ID, v => v.Name);
}