从字典返回值 [英] Return a value from Dictionary

查看:107
本文介绍了从字典返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天

我用字典创建了一个方法.字典的数据来自存储过程.

数据如下:

Good day

I have created a method with a dictionary. The data of the dictionary is coming from a stored procedure.

The data is as follows:

ReplacementStatusID      Description
1	                 Active	
2	                 Logged
3	                 Head Office
4	                 African Resonance	
5	                 Terminal Replace	
6	                 Inactive	




//字典的方法




//Method for the dictionary

public void dctRplStatus()
       {
               Dictionary<string, int> objdic = new Dictionary<string, int>();
               objdic = clsIngadmin.SelectdicReplacementStatus();
               cmbRplStatus.DisplayMember = "Key";
               cmbRplStatus.ValueMember = "Value";
               cmbRplStatus.DataSource = new BindingSource(objdic, null);
               objdic = null;
           }
          }



如何返回字典中的特定值.例如,我想返回可以显示在消息框中的已记录".

Thanx



How can I return a speceifc value that are inside of the dictionary. For example I want to return "Logged" that can display in a messagbox.

Thanx

推荐答案


我假设您要在字典中返回Key而不是Value,如果是这样,则可以使用以下lambada表达式返回"Logged",例如:

Hi,
I presume you want to return Key rather than the Value in your dictionary, if so, you can use the following lambada expression to return "Logged" for example:

object obj = objdic.Where(item => item.Value == 2).First().Key;




它返回"Logged",这是字典中的键之一.
我希望它能起作用!

干杯,




it return "Logged" which is one of the keys in your dictionary.
I hope it works!

Cheers,


最简单的方法是使用内置的ContainsValue 方法 [^ ] .
例如. Console.Writeline(objdic.ContainsValue("Logged"));

另一种方法是在字典上使用LINQ.
The easiest way would be to use the inbuilt ContainsValue method[^].
E.g. Console.Writeline(objdic.ContainsValue("Logged"));

Another way could be to use LINQ on the dictionary.


这篇关于从字典返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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