Reputation: 73
What expression can return a boolean if any dictionary within a list contains a particular value?
Imagine this, but without having to reference each item in the list individually:
(Dict_List(0).ContainsValue(value) or Dict_List(1).ContainsValue(value) or Dict_List(2).ContainsValue(value))=False
Ideally, if the value is not already in one of the dictionaries, along with several other conditions; then the code would add another dictionary. I know a foreach might work in this case, but a single expression would be faster at runtime, and it would be nice to know this just as a best practice.
Upvotes: 0
Views: 1419
Reputation: 54417
There are two LINQ options that come to mind:
If Dict_List.SelectMany(Function(d) d.Values).Contains(value) Then
or:
If Dict_List.Any(Function(d) d.ContainsValue(value)) Then
The first option gets the values from each Dictionary
and concatenates them all into a single list, then checks that list for a matching value. The second option checks each Dictionary
individually and returns True
if any individual check returns True
. Being LINQ, they both avoid pointlessly checking the rest of the values if a match is found.
Upvotes: 2