Using the extension method system. LINQ. enumerable. Distinct ()
For a simple data type, you can simply call this function, because it uses the default comparator (system. Collections. Generic. equalitycomparer <t>. Default ).
Whether the default property check type T implements this system. iequatable <t> generic interface. If this attribute is implemented, an API containing iequatable <t> is returned. the implementation of the equals method is equalitycomparer <t>. Otherwise, it returns the equalitycomparer provided by T <t>.
For the class data type, you need to define an implementation class of the iequalitycomparer <t> interface, or directly implement system. iequatable <t> or iequalitycomparer <t> in the class.
Example 1:
private static List<EquityUniverse> DistinctEstimatesUniverse(List<EquityUniverse> universeList) { return universeList.Distinct(new UniverseComparer()).ToList(); }
public class UniverseComparer : IEqualityComparer<EquityUniverse> { public bool Equals(EquityUniverse x, EquityUniverse y) { if (Object.ReferenceEquals(x, y)) return true; return x.GlobalEntityId.Equals(y.GlobalEntityId) && x.CountryId.Equals(y.CountryId); } public int GetHashCode(EquityUniverse obj) { return obj.CountryId.GetHashCode() ^ obj.GlobalEntityId.GetHashCode(); } }