Why not just use LINQ?
C#:
// Or use SingleOrDefault(...) if there can ever only be one.
var person = Cities.SelectMany(city => city.People)
.FirstOrDefault(person => IsOK(person));
if (person != null)
{
...
}
VB.Net (my best attempt, I'm not as verbosed in it):
// Or use SingleOrDefault(...) if there can ever only be one.
Dim person = Cities.SelectMany(Function(city) city.People)
.FirstOrDefault(Function(person) IsOK(person));
If person Not Nothing Then
...
End If
If all you are trying to do is see if there are any IsOK(person)
, then use the Any(...)
extension method instead:
C#:
var isOK = Cities.SelectMany(city => city.People)
.Any(person => IsOK(person));
VB.Net (my best attempt, I'm not as verbosed in it):
Dim isOK = Cities.SelectMany(Function(city) city.People)
.Any(Function(person) IsOK(person));