If I understand correctly, you just want to return the Keys whose value is "AAA". I would do this with LINQ, available from the . NET 3.5 (if you could specify which one you use would be better).
var valores = from item in ObterCodigo()
where item.Value == "AAA"
select item.Key;
This returns only the keys. If you want to return the KeyValuePair<string, string>
, just select item
instead of item.Key
.
Another way, also with LINQ, is:
var valores = ObterCodigo()
.Where(item => item.Value == "AAA")
.Select(item => item.Key);
Note that this returns an instance of IEnumerable<string>
. Depending on which implementation of ComboBox
you are using, you will need to list it first. For this, just use the function ToList()
on the return.
Bonus: If you can’t use LINQ (either because you’re using an old version of the Framework or because your boss won’t let you -- it already happened to me -- follow a version without it -- much like it would be in Java):
Dictionary<string, string> codigos = new Dictionary<string, string>();
foreach (KeyValuePair item in ObterCodigo())
if (item.Value == "AAA")
codigos.Add(item.Key, item.Value);
By search, you mean that she presented only these two equal values (
"AAA"
) ?– Alexandre Marcondes
I wanted to do a search for "AAA" and return the values "02" and "03"
– cumpadi