C#字典与lt的比较;字符串,int>;和int结果

C#字典与lt的比较;字符串,int>;和int结果,c#,dictionary,compare,C#,Dictionary,Compare,这是我创建字典的函数: public int Scale(string value) { //some calculs int result = 19; // or int between 0 and 40 this.stringToInt = new Dictionary<string, int>() { {"1p",00},{"2p",01},{"3p",03} ... {"300p",4

这是我创建字典的函数:

public int Scale(string value)
{
 //some calculs
 int result = 19; // or int between 0 and 40
 this.stringToInt = new Dictionary<string, int>()
 {
  {"1p",00},{"2p",01},{"3p",03} ... {"300p",40}
 };
// Here I try to do something like that: if(result == (int in dictionary) return associate string
}
我不知道如何返回关联字符串

提前感谢您的帮助

var-keyValuePair=this.stringToInt.FirstOrDefault(x=>x.Value==result);
var keyValuePair = this.stringToInt.FirstOrDefault(x => x.Value == result);
if (!keyValuePair.Equals(new KeyValuePair<string, int>()))
{
    // found, use keyValuePair.Key
}
如果(!keyValuePair.Equals(新的keyValuePair())) { //找到,请使用keyValuePair.Key }
我想使用Linq也行。但只返回第一个找到的值。如果该值在字典中不止一次,则只返回找到的第一个值


但是你应该像在
@nanu_nana

的评论中那样做,你必须在字典
新字典
中交换int和string,然后使用
.ContainsKey()
如果@nanu_nana建议不起作用(如果int值不是唯一的),你可以使用一个简单的linq:return stringToInt(x=>x.值==结果)但是,如果它们不是唯一的,你将得到匹配的第一个字符串键。如果使用的话,理想地使用CaveSKEY是最好的解决方案。最后,这是一个向后使用字典。如果你需要偶尔这样做,为适度的大小设置,罚款-但如果你需要经常这样做,或对于非常大的数据:你应该考虑。有两本字典,每本一本direction@nanu_nana或者
TryGetValue
似乎没有“the”搜索方向。在按键搜索值的过程中,这里是按键搜索值。因此,您可能需要一些新的数据结构,其中键是键和值的组合,并根据两者计算哈希值。应该注意的是,这是通过字典的线性搜索,就好像它是一个简单的列表。字典通常会给你带来的任何性能优势都是这样的。
var keyValuePair = this.stringToInt.FirstOrDefault(x => x.Value == result);
if (!keyValuePair.Equals(new KeyValuePair<string, int>()))
{
    // found, use keyValuePair.Key
}