简体   繁体   中英

C# - Best way to set Value of Dictionary'Key in Dictionary?

I have a Dictionary as below:

var mapStudent = new Dictionary<string, Dictionary<List<Student>, Boolean>>;

Now I want to set Boolean = true , I wrote below:

...
var key_value = mapStudent.Where(st => st.Key == classID).First();
var listStudent_EditFlag = key_value.Value;
foreach (KeyValuePair<List<Student>, Boolean> pair in listStudent_EditFlag)
{
    listStudent_EditFlag[pair.Key] = true;
    break;
}
...

Doing so is the better practice? Thanks in advance.

I'm sorry man you ain't going far with your code if you try to deal with such a structure. Since you are talking about best practices, I suggest you to rethink your structure to avoid endless headaches.

If you need to map a list of students to a single property, then maybe you actually need an abstraction over that list of students... i bet we are talking about a class (a school class). This way if you have to deal with a new property of the class tomorrow, you don't have to rewrite all of your code, but just add a property to the SchoolClass class. And to initilize all of your booleans to true you just need to set true as the default value for your property inside the constructor of SchoolClass.

Thinking the same way you probably need to abstract a list of ShoolClass mapped to a string to something like a School.

This way the code simplifies and it easier to leverage the power of Linq to perform complex operation and other technologies and of course present the data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM