Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I work with .NET Framework 4.5, linq, using MVC. Now I must change the code in the controller.

I don't know how to get and add new values from dictionary.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
348 views
Welcome To Ask or Share your Answers For Others

1 Answer

This will let you loop over your dictionary values:

foreach (string key in dictionary.Keys) {
    string value = dictionary[key];
    //Now, do something with value, such as add to database
}

And this will let you find a specific dictionary value:

string key = "a_key";
if (dictionary.ContainsKey(key)) {
    string value = dictionary[key];
    //Now do something with value
}

I think you are looking to do something like the second example above.

What's confusing about your code example is that you have !dictionary.ContainsKey, which means that the key does not exist in the dictionary, but your comment says //here must be these new key-value from dictionary. This makes me think that maybe you intend for dictionary.ContainsKey rather than !dictionary.ContainsKey and that you then want to retrieve the value and do something with it. But I'm not sure.

In any case, perhaps you are asking simply how to get the value out of the dictionary, and in that case, you may find the dictionary[key] notation using brackets to be useful. Is that what you were going for?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...