fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class Sample
  6. {
  7. public string Value1 { get; set; }
  8. public string Value2 { get; set; }
  9. public override string ToString() {
  10. return Value1 + "/" + Value2;
  11. }
  12. }
  13.  
  14. class SampleComparer : IEqualityComparer<Sample>
  15. {
  16. public bool Equals(Sample x, Sample y)
  17. {
  18. return x.Value2 == y.Value2;
  19. }
  20. public int GetHashCode(Sample obj)
  21. {
  22. return obj.Value2.GetHashCode();
  23. }
  24. }
  25.  
  26. public class Test
  27. {
  28. public static void Main()
  29. {
  30. var list = new List<Sample>() {
  31. new Sample{ Value1 = "Apple", Value2="Red" },
  32. new Sample{ Value1 = "Banana", Value2="Yellow" },
  33. new Sample{ Value1 = "Lemon", Value2="Yellow" },
  34. };
  35.  
  36. list = list.Distinct(new SampleComparer()).ToList();
  37.  
  38. foreach (var n in list) {
  39. Console.WriteLine(n);
  40. }
  41. }
  42. }
Success #stdin #stdout 0.03s 33928KB
stdin
Standard input is empty
stdout
Apple/Red
Banana/Yellow