Disclaimer: this solution doesn't specifically handle the same BaseCode
with different/same locations; you didn't mention anything about this in your requirements.
IEqualityComparer<T>
Route
The important parts here are the IEqualityComparer<T>
implementations for both Person
and Location
:
class Program
{
static void Main(string[] args)
{
var p1 = new Person {Name ="John", BaseCode="AA12", Locations = new List<Location>
{
new Location { Name = "India" },
new Location { Name = "USA" }
}};
var p2 = new Person {Name ="John", BaseCode="AA13", Locations = new List<Location>
{
new Location { Name = "India" },
new Location { Name = "USA" }
}};
var p3 = new Person {Name ="John", BaseCode="AA14", Locations = new List<Location>
{
new Location { Name = "India" },
new Location { Name = "UK" }
}};
var persons = new List<Person> { p1, p2, p3 };
// Will not return p2.
var distinctPersons = persons.Distinct(new PersonComparer()).ToList();
Console.ReadLine();
}
}
public class PersonComparer : IEqualityComparer<Person>
{
public bool Equals(Person x, Person y)
{
if (x == null || y == null)
return false;
bool samePerson = x.Name == y.Name;
bool sameLocations = !x.Locations
.Except(y.Locations, new LocationComparer())
.Any();
return samePerson && sameLocations;
}
public int GetHashCode(Person obj)
{
return obj.Name.GetHashCode();
}
}
public class LocationComparer : IEqualityComparer<Location>
{
public bool Equals(Location x, Location y)
{
if (x == null || y == null)
return false;
return x.Name == y.Name;
}
public int GetHashCode(Location obj)
{
return obj.Name.GetHashCode();
}
}
The PersonComparer
uses the linq Except
extension supplying the LocationComparer
to produce a list of differences between two lists of locations.
The PersonComparer
then feeds into the linq Distinct
method.
IEquatable<T>
Route
If you need to work with BaseCode
being different counting towards being a "match", I don't think this route would work because of GetHashCode
not giving you an opportunity to distinguish values.
An alternative solution is to implement IEquatable<T>
on the classes themselves and also override GetHashCode
, Distinct
and Except
will then honour this implementation:
public class Person : IEquatable<Person>
{
public string Name { get; set; }
public string BaseCode { get; set; }
public List<Location> Locations { get; set; }
public bool Equals(Person other)
{
if (other == null)
return false;
bool samePerson = Name == other.Name;
// This is simpler because of IEquatable<Location>
bool sameLocations = !Locations.Except(other.Locations).Any();
return samePerson && sameLocations;
}
public override int GetHashCode()
{
return Name.GetHashCode();
}
}
public class Location : IEquatable<Location>
{
public string Name { get; set; }
public bool Equals(Location other)
{
if (other == null)
return false;
return Name == other.Name;
}
public override int GetHashCode()
{
return Name.GetHashCode();
}
}
Which results in a simpler call:
var distinctPersons = persons.Distinct().ToList();