如何使用linq使用字典更新数据库 [英] How to update database using dictionary with linq

查看:64
本文介绍了如何使用linq使用字典更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MVC使用.NET Framework 4.5 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.

推荐答案

这将使您遍历字典值:

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.

您的代码示例令人困惑的是您拥有!dictionary.ContainsKey,这意味着该键在词典中不存在,但是您的注释为//here must be these new key-value from dictionary.这使我认为也许您打算使用dictionary.ContainsKey而不是!dictionary.ContainsKey,然后又想检索该值并对其进行处理.但是我不确定.

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.

在任何情况下,也许您只是在问如何从字典中获取值,在这种情况下,您可能会发现使用方括号的dictionary[key]表示法很有用.那是你要去的吗?

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?

这篇关于如何使用linq使用字典更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆