我如何得到一个字典键值在c# ?
Dictionary<string, string> types = new Dictionary<string, string>()
{
{"1", "one"},
{"2", "two"},
{"3", "three"}
};
我想要这样的东西:
getByValueKey(string value);
getByValueKey("one")必须返回"1"。
最好的方法是什么?也许是哈希表或排序列表?
我如何得到一个字典键值在c# ?
Dictionary<string, string> types = new Dictionary<string, string>()
{
{"1", "one"},
{"2", "two"},
{"3", "three"}
};
我想要这样的东西:
getByValueKey(string value);
getByValueKey("one")必须返回"1"。
最好的方法是什么?也许是哈希表或排序列表?
当前回答
我遇到的情况是LINQ绑定不可用,必须显式展开lambda。它的结果是一个简单的函数:
public static T KeyByValue<T, W>(this Dictionary<T, W> dict, W val)
{
T key = default;
foreach (KeyValuePair<T, W> pair in dict)
{
if (EqualityComparer<W>.Default.Equals(pair.Value, val))
{
key = pair.Key;
break;
}
}
return key;
}
这样称呼它:
public static void Main()
{
Dictionary<string, string> dict = new Dictionary<string, string>()
{
{"1", "one"},
{"2", "two"},
{"3", "three"}
};
string key = dict.KeyByValue("two");
Console.WriteLine("Key: " + key);
}
它适用于。net 2.0和其他有限的环境。
其他回答
Dictionary<K,V>扩展。我已经用了很长时间了::
public static bool TryGetKey<K, V>(this IDictionary<K, V> instance, V value, out K key)
{
foreach (var entry in instance)
{
if (!entry.Value.Equals(value))
{
continue;
}
key = entry.Key;
return true;
}
key = default(K);
return false;
}
并使用as:
public static void Main()
{
Dictionary<string, string> dict = new Dictionary<string, string>()
{
{"1", "one"},
{"2", "two"},
{"3", "three"}
};
string value="two";
if (dict.TryGetKey(value, out var returnedKey))
Console.WriteLine($"Found Key {returnedKey}");
else
Console.WriteLine($"No key found for value {value}");
}
我遇到的情况是LINQ绑定不可用,必须显式展开lambda。它的结果是一个简单的函数:
public static T KeyByValue<T, W>(this Dictionary<T, W> dict, W val)
{
T key = default;
foreach (KeyValuePair<T, W> pair in dict)
{
if (EqualityComparer<W>.Default.Equals(pair.Value, val))
{
key = pair.Key;
break;
}
}
return key;
}
这样称呼它:
public static void Main()
{
Dictionary<string, string> dict = new Dictionary<string, string>()
{
{"1", "one"},
{"2", "two"},
{"3", "three"}
};
string key = dict.KeyByValue("two");
Console.WriteLine("Key: " + key);
}
它适用于。net 2.0和其他有限的环境。
types.Values.ToList().IndexOf("one");
values . tolist()将您的字典值转换为对象列表。 IndexOf("one")搜索你的新列表,寻找"one",并返回与字典中Key/Value对的索引匹配的Index。
这个方法并不关心字典中的键,它只是返回您正在寻找的值的索引。
请记住,在你的字典中可能有不止一个“1”值。这就是没有“get key”方法的原因。
我有一个很简单的方法。这对我来说很完美。
Dictionary<string, string> types = new Dictionary<string, string>();
types.Add("1", "one");
types.Add("2", "two");
types.Add("3", "three");
Console.WriteLine("Please type a key to show its value: ");
string rLine = Console.ReadLine();
if(types.ContainsKey(rLine))
{
string value_For_Key = types[rLine];
Console.WriteLine("Value for " + rLine + " is" + value_For_Key);
}
keys中的键的顺序是未指定的,但它与values中的相关值相同(来自c#文档)。
因此,(在某些情况下)对值的集合执行此操作的有效方法如下:
/// <summary>
/// Gets the 1st key matching each value
/// </summary>
public static IEnumerable<TKey> GetKeys<TKey,TValue>(this Dictionary<TKey, TValue> dic, IEnumerable<TValue> values) where TKey : notnull
{
//The order of the keys in Keys is unspecified, but it is the same as the associated values in Values
var dicKeys = dic.Keys.ToList();
var dicValues = dic.Values.ToList();
foreach (var value in values)
{
var i = dicValues.IndexOf(value); //Will return the index of the 1st found value, even when multiple values are present
//we could test if i==-1 there.
yield return dicKeys[i];
}
}