'Linq -> Compare groups in one list

I have one object like that Person(Id, DistrictId, CountryId)

I have one list group by DistrictID for example:

var List<Person> personList = new List<Person>();
personList.Add(new Person(1,1,1)):
personList.Add(new Person(2,1,2));
personList.Add(new Person(3,1,1));
personList.Add(new Person(1,2,1));
personList.Add(new Person(2,2,2));
personList.Add(new Person(3,2,3));
personList.Add(new Person(4,2,3));
personList.Add(new Person(1,3,2));
personList.Add(new Person(2,3,1));

var personsByDistrict = personList.GroupBy(dt => dt.DistrictId).Select(x => x.ToList()).ToList();

-DistrictOne

  • Person1(1,1,1);Person2(2,1,2);Person3(3,1,1);
    

-DistrictTwo

  • Person4(1,2,1);Person5(2,2,2);Person6(3,2,3);Person7(4,2,3);
    

-DistrictThree

  • Person8(1,3,2);Person9(2,3,1)
    

I need compare all groups and add in one final list all Persons to share the exactly the same countries. The final list should be:

-DistrictOne

  • Person1(1,1,1);Person2(2,1,2);Person3(3,1,1);
    

-DistrictThree

  • Person8(1,3,2);Person9(2,3,1)
    


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source