Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
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;
           }
          }


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
Posted
Updated 2-Apr-12 2:08am
v2

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:

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



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

Cheers,
 
Share this answer
 
Comments
JacoBosch 2-Apr-12 8:45am    
Thanx Reza!
Reza Ahmadi 2-Apr-12 10:22am    
Your Welcome!
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.
 
Share this answer
 
Comments
JacoBosch 2-Apr-12 8:19am    
Must I write anothor method and call the dictionary inside the method with the ContainsValue method?
Abhinav S 2-Apr-12 12:33pm    
No that is not required. You can do this within the same method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900