C# 从值中获取键 - Dictionary<string, List<string>>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16860643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get key from value - Dictionary<string, List<string>>
提问by Krishh
I am having trouble getting the key by specifying a value. What is the best way I can achieve this?
我无法通过指定值来获取密钥。我可以实现这一目标的最佳方法是什么?
var st1= new List<string> { "NY", "CT", "ME" };
var st2= new List<string> { "KY", "TN", "SC" };
var st3= new List<string> { "TX", "OK", "MO" };
var statesToEmailDictionary = new Dictionary<string, List<string>>();
statesToEmailDictionary.Add("[email protected]", st1);
statesToEmailDictionary.Add("[email protected]", st2);
statesToEmailDictionary.Add("[email protected]", st3);
var emailAdd = statesToEmailDictionary.FirstOrDefault(x => x.Value.Where(y => y.Contains(state))).Key;
采纳答案by p.s.w.g
The return value from FirstOrDefaultwill be a KeyValuePair<string, List<string>>, so to get the key, simply use the Keyproperty. Like this:
来自的返回值FirstOrDefault将是 a KeyValuePair<string, List<string>>,因此要获取密钥,只需使用该Key属性即可。像这样:
var emailAdd = statesToEmailDictionary
.FirstOrDefault(x => x.Value.Contains(state))
.Key;
Alternatively, here's the equivalent in query syntax:
或者,这是查询语法中的等效项:
var emailAdd =
(from p in statesToEmailDictionary
where p.Value.Contains(state)
select p.Key)
.FirstOrDefault();
回答by Joe Enos
var emailAdd = statesToEmailDictionary
.FirstOrDefault(x => x.Value != null && x.Value.Contains(state))
.Key;
But if you're looking for performance, I'd suggest reversing your dictionary and creating a dictionary of <state, email>to do what you're looking for.
但是如果您正在寻找性能,我建议您反转您的字典并创建一个字典<state, email>来做您正在寻找的事情。
// To handle when it's not in the results
string emailAdd2 = null;
foreach (var kvp in statesToEmailDictionary)
{
if (kvp.Value != null && kvp.Value.Contains(state))
{
emailAdd2 = kvp.Key;
break;
}
}
回答by SamiHuutoniemi
var temp = statesToEmailDictionary.Where( x => x.Value.Contains(state)).FirstOrDefault();
var emailAdd = temp != null ? temp.Key : string.Empty;
回答by Adam
I think you want:
我想你想要:
var emailAdd = statesToEmailDictionary.FirstOrDefault(x => x.Value.Any(y => y.Contains(state))).Key;
回答by erapert
What everyone in this thread failed to mention is that the FirstOrDefaultmethod is only available through Linq:
该线程中的每个人都没有提到该FirstOrDefault方法只能通过Linq 使用:
using System;
using System.Collections.Generic;
// FirstOrDefault is part of the Linq API
using System.Linq;
namespace Foo {
class Program {
static void main (string [] args) {
var d = new Dictionary<string, string> () {
{ "one", "first" },
{ "two", "second" },
{ "three", "third" }
};
Console.WriteLine (d.FirstOrDefault (x => x.Value == "second").Key);
}
}
}
回答by Christian Alain Ouellet
Simple Linq to do just that
简单的 Linq 做到这一点
Dim mKP = (From mType As KeyValuePair(Of <Key type>, <Value type>) In <Dictionary>
Where mType.Value = <value seeked> Select mType).ToList
If mKP.Count > 0 then
Dim value as <value type> = mKP.First.Value
Dim key as <Key type> = mKP.First.Key
End if
Of course if there are duplicate values this will return more than one KeyValuePair
当然,如果有重复的值,这将返回多个 KeyValuePair
回答by G.J
var emailAdd = statesToEmailDictionary.First(x=>x.Value.Contains(state)).Key;

