Note: this code was written quickly, and, while it compiles, I have not tested it thoroughly.
First, if your code is creating the list of instances of 'Person and you want to enforce some screening rule about names, here's an example:
public class Person
{
private static List<string> surnamesInUse = new List<string>();
private static List<string> firstNamesInUse = new List<string>();
private string lname;
private string lsurname;
public Person(string name, string surname)
{
if (name == String.Empty || surname == String.Empty)
{
throw new ArgumentException($"name and surname cannot be empty strings");
}
lname = name.ToLower();
lsurname = surname.ToLower();
if (lname == lsurname)
{
throw new ArgumentException($"name and surname cannot match independent of case");
}
if (firstNamesInUse.Contains(lname))
{
throw new DuplicateNameException($"{name} is already in use as a first name");
}
if (firstNamesInUse.Contains(lsurname))
{
throw new DuplicateNameException($"{surname} is already in use as a first name");
}
if (surnamesInUse.Contains(lname))
{
throw new DuplicateNameException($"{name} is already in use as a surname");
}
if (surnamesInUse.Contains(lsurname))
{
throw new DuplicateNameException($"{surname} is already in use as a surname");
}
surnamesInUse.Add(lsurname);
firstNamesInUse.Add(lname);
Name = name;
Surname = surname;
}
public string Name { get; set; }
public string Surname { get; set; }
public bool Equals(Person p2)
{
return
lname == p2.Name.ToLower()
&&
lsurname == p2.Surname.ToLower();
}
}
If you did not create the instances of 'Person, and want to look for matches, here's some ideas:
public enum MatchType
{
NoMatch,
SurnameMatch,
FirstNameMatch,
BothNameMatch
}
public static class PersonMatcher
{
public static MatchType ComparePersons(Person p1, Person p2)
{
if (p1.Surname == p2.Surname)
{
if (p1.Name == p2.Name) return MatchType.BothNameMatch;
return MatchType.SurnameMatch;
}
if (p1.Name == p2.Name) return MatchType.FirstNameMatch;
return MatchType.NoMatch;
}
public static bool IsUnique(Person p1, List<Person> persons)
{
return ! persons.Any(p2 => (! p2.Equals(p1)) && ComparePersons(p2, p1) != MatchType.NoMatch);
}
public static IEnumerable<Person> GetDistinct(List<Person> persons)
{
return persons.Where(p1 => IsUnique(p1, persons));
}
public static List<Person> GetNotMatching(Person p1, List<Person> ps)
{
return ps.Where(p2 => (!p2.Equals(p1)) && ComparePersons(p1, p2) == MatchType.NoMatch).ToList();
}
public static List<Person> GetSurnameMatches(Person p1, List<Person> ps)
{
return ps.Where(p2 => (!p2.Equals(p1)) && ComparePersons(p1, p2) == MatchType.SurnameMatch).ToList();
}
public static List<Person> GetFirstNameMatches(Person p1, List<Person> ps)
{
return ps.Where(p2 => (!p2.Equals(p1)) && ComparePersons(p1, p2) == MatchType.FirstNameMatch).ToList();
}
public static List<Person> GetDuplicates(Person p1, List<Person> ps)
{
return ps.Where(p2 => (!p2.Equals(p1)) && ComparePersons(p1, p2) == MatchType.BothNameMatch).ToList();
}
}
You could easily turn these methods into Extension Methods, and/or modify to compare
without case sensitivity via conversion to lower-case.