Contains <T> () and how to implement it
Say class
Person
+Name: string
+Contacts: List<Person>
I want to check if a person has contact with a specific name without creating an instance of a fictitious person.
person.Contacts.Contains<string>("aPersonName");
This should check all persons in the contact list if their Name.Equals ("aPersonName"); I see that there is already available, but I do not know where I should implement its logic.
+3
5 answers
Perhaps the easiest way is to use Enumerable.Any :
return person.Contacts.Any(person => person.Name=="aPersonName");
Alternatively, execute a project, and then:
return person.Select(person => person.Name).Contains("aPersonName");
+12
I assume Contacts are contacts for this person (the person in your code snippet)
contains, T true false, . , , IList.Exists , .
example (# 3.0)
bool hasContact = person.Contacts.Exists(p => p.Name == "aPersonName");
(# 2.0)
bool hasContact = person.Contacts.Exists(delegate(Person p){ return p.Name == "aPersonName"; });
+2