You can project people to sequence of Email-Person pairs and group these sequence by email address. In this case each group will have people which share same email address (key of group):
people.SelectMany(p => p.Emails.Select(e => new { e.Address, Person = p }))
.GroupBy(x => x.Address)
.Select(g => new { Address = g.Key, People = g.Select(x => x.Person) })
Query syntax:
from p in people
from e in p.Emails
select new { e.Address, Person = p } into x
group x by x.Address into g
select new {
Address = g.Key,
People = g.Select(x => x.Person)
}
NOTE: If you need whole email to be unique, then override Equals
and GetHashCode
methods of Email
in order to compare instances of this class by values of its fields instead of by reference (or make sure you are share same instances of email between different people).