Click here to Skip to main content
13,003,589 members (62,113 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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.

Posted 2-Apr-12 2:00am
Updated 2-Apr-12 2:08am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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!

JacoBosch 2-Apr-12 8:45am
Thanx Reza!
Reza Ahmadi 2-Apr-12 10:22am
Your Welcome!
Rate this: bad
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.
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170626.1 | Last Updated 2 Apr 2012
Copyright © CodeProject, 1999-2017
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