Dictionary Search

Asked

Viewed 4,107 times

7

I have the following Dictionary:

public static Dictionary<string, string> ObterCodigo()
{
    return new Dictionary<string, string>
       {
           {"18", "Teste"},     
           {"19", "Teste"},
           {"02", "AAA"},
           {"03", "AAA"},
           {"109", "BBB"},
           {"157", "BBB"},
       };
}

I would like my combobox to search only the values of "AAA". It has how to do?

  • By search, you mean that she presented only these two equal values ("AAA") ?

  • I wanted to do a search for "AAA" and return the values "02" and "03"

2 answers

9


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);
  • 2

    Using the Diagnostics.StopWatch I saw that the second code in your answer is much ( but much ) faster. But it’s still the right answer.

  • 1

    Hmm, I didn’t know that using syntatic sugar from LINQ caused performance problems. Thanks for the observation.

  • Probably because a LINQ expression is not performed immediately. Experiment with ". Tolist()" joined at the end of the expression.

  • @mdisibio The method Select also has delayed execution. The two excerpts theoretically do exactly the same thing.

0

You can assemble a dictionary part just for that.

You can populate it as follows:

Dictionary<string, string> newDic = new Dictionary<string, string>();
Dictionary<string, string> oldDic = ObterCodigo();
foreach (string key in oldDic.Keys)
{
    if (oldDic[key] == "AAA") newDic.add(key, oldDic[key]);
}

At the end of the above section, the dictionary newDic will contain only the desired items.

  • Are you sure a List<KeyValuePair<string, string>> is a better alternative than Dictionary<string, string>? In essence, Dictionary already does the work of a list of KeyValuePairs.

  • 1

    @Andréleria is right. I will rewrite the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.