Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have a model class

public class studentData
{
    public string ID{ get; set; }
    public string Name{ get; set; }

}


i create a List and assign the values to this model
sample code:-

  List<studentData> std=new <studentData>{
new student{ID="1",Name="teststudent1"},
new student{ID="2",Name="teststudent2"},
}


now i need to convert this list into "dictionary <string,string>"

i need my list into dictionary look like :-

"studentdata": [
{
"ID": "1",
"Name": "teststudent1"
},
{
"ID": "2",
"Name": "teststudent2"
}
]

can any one suggest me the solution.

What I have tried:

i tried this but it's only working when in my list value are exsits only for index[0] if value are in more then 1 index in the list then it's not working.

var item = JsonConvert.SerializeObject(StudentList);
item = item.Replace("[", "").Replace("]","");
var vendordata = JsonConvert.DeserializeObject<dictionary<string, dynamic>>(item);
Posted
Updated 19-Sep-22 22:08pm
v6
Comments
Richard MacCutchan 19-Sep-22 6:27am    
Does List<T> have a ToDictionary method? And what problem are you trying to solve?
TCS54321 19-Sep-22 6:37am    
i want to convert my List into Dictionary. check my question. i updated my question
Richard MacCutchan 19-Sep-22 6:43am    
Yes but you ignored both of my questions.
TCS54321 19-Sep-22 6:49am    
i did net get my list data into Dictionary. i used this code to convert list into dictionary
----->>> Dictionary<studentdata, studentdata=""> dict = mylist.ToDictionary(x=>x);

1 solution

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:
C#
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);
    }
 
Share this answer
 
v2
Comments
TCS54321 20-Sep-22 4:00am    
thanx for you reply but your solution does not work in my case. i need the output look like this --->

"studentdata": [
{
"ID": "1",
"Name": "teststudent1"
},
{
"ID": "2",
"Name": "teststudent2"
}
]
TCS54321 20-Sep-22 4:04am    
(ID, Name) should be a Key and (1,teststudent1/2,teststudent2) should be a value
OriginalGriff 20-Sep-22 4:24am    
Then change it to do so: but remember that if your key is a class, then unless you create a class and implement comparison operators the reference comparison will be used, not the class property values. Since you will be creating new instances for each row, the references will be different for each even if the ID and Name are identical!

I think you need to think about why you are doing this - it sounds a lot like an over-thought solution from over here!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900