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?
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With