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. public override int GetHashCode()
  14. {
  15. return Value2.GetHashCode();
  16. }
  17. public override bool Equals(object obj)
  18. {
  19. var other = obj as Sample;
  20. return other == null ? false : this.Value2 == other.Value2;
  21. }
  22. }
  23.  
  24. public class Test
  25. {
  26. public static void Main()
  27. {
  28. var list = new List<Sample>() {
  29. new Sample{ Value1 = "Apple", Value2="Red" },
  30. new Sample{ Value1 = "Banana", Value2="Yellow" },
  31. new Sample{ Value1 = "Lemon", Value2="Yellow" },
  32. };
  33.  
  34. list = list.Distinct().ToList();
  35.  
  36. foreach (var n in list) {
  37. Console.WriteLine(n);
  38. }
  39. }
  40. }
Success #stdin #stdout 0.04s 33936KB
stdin
Standard input is empty
stdout
Apple/Red
Banana/Yellow