c# - How can I find the key when I have to value using namevaluecollection? -
i google'd around twenty minutes. forgive me if duplicate.
so reading app.config file , have bunch of keys/value pairs
{'a','1'},{'b','5'},{'c','9'} ... etc
finding values when give keys easy. going reverse proves more difficult thought would.
strangely api on msdn not seem mention reverse search.
how find value of key if given "1" if allowed @ all?
the nicest way can think use linq on allkeys
property:
mynamevaluecollection.allkeys.first(key => mynamevaluecollection[key] == myvalue);
it unfortunate namevaluecollection
not implement ienumerable<keyvaluepair<string, string>>
.
i see comments you're not familiar linq. i've provided links in comments going. particular linq query approximately equivalent following, written tersely:
foreach(var key in mynamevaluecollection.allkeys) { var value = mynamevaluecollection.allkeys[key]; if (value == myvalue) { return value; } }
Comments
Post a Comment