List<Person> list_persons = new List<Person>(
new Person("Lucy",22,"woman"),
new Person("Lily",23,"woman"),
new Person("Tom",24,"man"),
new Person("Lucy",22,"woman"),
new Person("Lily",23,"woman"),
new Person("LiLei",25,"man")
);
如同上表中,名字(name)中重復的想要去除,使用linq進行去重的方法,使用Distinct()根本無法達到要求。那么:
var list_distinct = list_Persons.GroupBy(c => c.name).Select(c => c.First());
實際的意思是根據某一列進行分組,然后獲取每一組的第一條數據,可以解決此次需求