I have a dictionary of type
Dictionary
I want to return the first instance where a condition is met using
var
If you just care about existence, you could use ContainsValue(0)
or Any(p => p.Value == 0)
instead? Searching by value is unusual for a Dictionary<,>
; if you were searching by key, you could use TryGetValue
.
One other approach:
var record = data.Where(p => p.Value == 1)
.Select(p => new { Key = p.Key, Value = p.Value })
.FirstOrDefault();
This returns a class - so will be null
if not found.