Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

c# - How to update database using dictionary with linq

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

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

1 Reply

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...