如何使用.Where来访问字典中的值?

som*_*ool -1 c# linq where-clause

我只想要id等于"zj"的任何值

Dictionary<string,string> z = dm.UpdateAndReturnIdol(User, id, v).where(keys.equals("zj"));
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 5

如果返回类型是Dictionary<string, string>那么你可以只查找值,例如

Dictionary<string, string> dictionary =  dm.UpdateAndReturnIdol(User, id, v);
string value = dictionary["zj"];
Run Code Online (Sandbox Code Playgroud)

或者如果你不知道是否会有一个条目:

Dictionary<string, string> dictionary =  dm.UpdateAndReturnIdol(User, id, v);
string value;
if (dictionary.TryGetValue("zj", out value))
{
    // Use the value
}
Run Code Online (Sandbox Code Playgroud)

有没有必要用Where在这里-整字典的是,你可以通过按键有效查找...并只能有每个键一个值,所以没有必要得到一个序列值.