The Linq cell is annoying, you need to implement the IEqualityComparer interface in the class you want to distinguish (), then the way you get your highlighted selection that you have in mind will be:
IEnumerable<Person> people = GetPeople(); people.SelectMany((person) => person.LastName).Distinct();
SelectMany () aligns the result of combining the enumerated elements, so you don't get IEnumerable <IEnumerable <State → gt; but get IEnumerable <State>
When implementing IEqualityComparer, it is known that Distinct () checks both results from Equals () are equivalent, and the results from GetHashCode () are equivalent. I think in your case you want to implement a comparator in the State class.
It might look something like this:
public class State : IEqualityComparer<State> { int StateID {get;set;} string StateName {get;set;} public bool Equals(State x, State y) { return x.StateID == y.StateID && x.StateName == y.StateName; } public int GetHashCode(State obj) { return obj.StateId; } }
Remember that your separate () will not do anything for you if you do not implement IEqualityComparer.
source share