Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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;
           }
          }
 
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 2-Apr-12 3:00am
JacoBosch1.4K
Edited 2-Apr-12 3:08am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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;
 

it return "Logged" which is one of the keys in your dictionary.
I hope it works!
 
Cheers,
  Permalink  
Comments
JacoBosch at 2-Apr-12 8:45am
   
Thanx Reza!
Reza Ahmadi at 2-Apr-12 10:22am
   
Your Welcome!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
JacoBosch at 2-Apr-12 8:19am
   
Must I write anothor method and call the dictionary inside the method with the ContainsValue method?
Abhinav S at 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)

  Print Answers RSS
0 OriginalGriff 431
1 Afzaal Ahmad Zeeshan 184
2 /\jmot 176
3 Sergey Alexandrovich Kryukov 175
4 Marcin Kozub 175
0 OriginalGriff 8,344
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,634
3 Maciej Los 5,024
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 2 Apr 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100