List<TheRecord> list = new(); list.Add(new TheRecord() { Name = "Bob", Age = 30 }); list.Add(new TheRecord() { Name = "Bob", Age = 30 }); list.Add(new TheRecord() { Name = "Mary", Age = 30 }); list.Add(new TheRecord() { Name = "Mary", Age = 60 });
var r1 = list.Distinct().ToList(); // result: // { Name = "Bob", Age = 30 } // { Name = "Bob", Age = 30 } // { Name = "Mary", Age = 30 } // { Name = "Mary", Age = 60 }
var r2 = list.DistinctBy(x => x.Name).ToList(); // result: // { Name = "Bob", Age = 30 } // { Name = "Mary", Age = 30 }
var r3 = list.DistinctBy(x => new { x.Name, x.Age }).ToList(); // result: // { Name = "Bob", Age = 30 } // { Name = "Mary", Age = 30 } // { Name = "Mary", Age = 60 }
List<string> s = new(); s.Add("a1"); s.Add("a1"); var r4 = s.Distinct().ToList(); // result: // { "a1" }
publicclassTheRecord { publicstring Name { get; set; } publicint Age { get; set; } }