0

Suppose I have this class:

class Person {
    string name;
}

Suppose I have a HashSet<Person> people with some objects in.

It is guaranteed that there will be at most 2 objects in people that have the same name.

Is there a way to find a pair of people with the same name inside people with Linq of another short way?

I could obviously run 2 fors, but I guess there may be a oneliner for that?

Daniel
  • 7,357
  • 7
  • 32
  • 84
  • 1
    maybe try to override the Equals? eg `public override bool Equals(object obj)` – jazb Jan 25 '22 at 03:57
  • Use GroupBy to group the objects by a property, then use Where on the resulting grouping to find groups with 2 or more objects. – John Glenn Jan 25 '22 at 05:29
  • Either add an `Equals` method to your `Person` class or use the `HashSet` constructor taking an `IEqualityComparer` instance and create a PersonNameEqualityComparer class implementing this interface. – ckuri Jan 25 '22 at 08:03

0 Answers0