Try the following:
If Not contacts.Any(Function(obj) obj.LastName = "smith") Then
Dim newContact As New Contact()
newContact.FirstName = "joe"
newContact.LastName = "smith"
newContact.PhoneNumber = "234345435"
newContact.Birthday = Date.Now.AddYears(-20)
contacts.Add(newContact)
End If
The bit in brackets after
Any
is a
Lambda expression[
^]
It's essentially saying - here is a
Function
that is going to take a parameter
obj
and with
obj
we want to look at the
LastName
property and compare it to the text
"smith"
(you would probably want to convert to lowercase or capitals before doing the comparison).
The compiler knows that
LastName
can be a property of
obj
because we are using
contacts.Any
and it knows that
contacts
is a
List
of
Contact
. We are using
.Any
because we don't actually need a list of matches, we just need to know
if there is a match.
And note the
Not
because the
.Any
will return
True
if it does find a match