views:

25

answers:

1

I have dictionary, like

Dictionary<string, bool> accValues = new Dictionary<string, bool>()

And I want to get bool value for specific key. I can do it via foreach, like

foreach (KeyValuePair<string, bool> keypair in accValues)
            {
                if (keypair.Key == "SomeString")
                {
                    return keypair.Value;
                }
            }

But how is it possible to realize using Where function?

+4  A: 

Why iterate over every key/value pair? Use

accValues["SomeString"]

or, if you don't want an exception to be thrown when no such key exists in the dictionary:

accValue.TryGetValue("SomeString", out boolValue)

if you want to find a value for a key that matches some arbitrary predicate, you can use a statement like this:

accValues.Where(kvp => kvp.Key == "SomeString").Select(kvp => kvp.Value).FirstOrDefault();
maciejkow