代码1:
using System; using System.Collections.Generic; using System.Linq; namespace LinqDistinctDemo { public class Person { public string Name { get; set; } public int Age { get; set; } public Person(string name, int age) { this.Name = name; this.Age = age; } } public class PersonCompare : IEqualityComparer<Person> { public bool Equals(Person x, Person y) { if (x == null || y == null) return false; Console.WriteLine("===========XName:{0} XAge:{1} XHashCode:{2} YName:{3} YAge:{4} YHashCode:{5}", x.Name, x.Age, x.GetHashCode(), y.Name, y.Age, y.GetHashCode()); return x.Name.Equals(y.Name) && x.Age == y.Age; } public int GetHashCode(Person obj) { // Console.WriteLine("GetHashCode Name:{0} Age:{1} HashCode:{2}", obj.Name, obj.Age, obj.GetHashCode()); //return obj.GetHashCode(); string s = string.Format("{0}_{1}", obj.Name, obj.Age); Console.WriteLine("-------------Name:{0} Age:{1} HashCode:{2}", obj.Name, obj.Age, s.GetHashCode()); return obj.GetHashCode(); } } class Program { static void Main(string[] args) { Person person = new Person("ZhangSan", 26); List<Person> personList = new List<Person>() { person, new Person("XiaoMing",25), new Person("CuiYanWei",25), new Person("XiaoMing",26), new Person("XiaoMing",25), new Person("LaoWang",26), new Person("XiaoMing",26), person }; List<Person> defaultDistinctPersons = personList.Distinct().ToList<Person>(); foreach (Person p in defaultDistinctPersons) { Console.WriteLine("Name:{0} Age:{1}", p.Name, p.Age); } Console.WriteLine("*******************"); List<Person> comparePersons = personList.Distinct(new PersonCompare()).ToList<Person>(); foreach (Person p in comparePersons) { Console.WriteLine("Name:{0} Age:{1}", p.Name, p.Age); } Console.ReadLine(); } } }
代码2:
using System; using System.Collections.Generic; using System.Linq; namespace LinqDistinctDemo { public class Person { public string Name { get; set; } public int Age { get; set; } public Person(string name, int age) { this.Name = name; this.Age = age; } } public class PersonCompare : IEqualityComparer<Person> { public bool Equals(Person x, Person y) { if (x == null || y == null) return false; Console.WriteLine("===========XName:{0} XAge:{1} XHashCode:{2} YName:{3} YAge:{4} YHashCode:{5}", x.Name, x.Age, x.GetHashCode(), y.Name, y.Age, y.GetHashCode()); return x.Name.Equals(y.Name) && x.Age == y.Age; } public int GetHashCode(Person obj)
{
return 0; } } class Program { static void Main(string[] args) { Person person = new Person("ZhangSan", 26); List<Person> personList = new List<Person>() { person, new Person("XiaoMing",25), new Person("CuiYanWei",25), new Person("XiaoMing",26), new Person("XiaoMing",25), new Person("LaoWang",26), new Person("XiaoMing",26), person }; List<Person> defaultDistinctPersons = personList.Distinct().ToList<Person>(); foreach (Person p in defaultDistinctPersons) { Console.WriteLine("Name:{0} Age:{1}", p.Name, p.Age); } Console.WriteLine("*******************"); List<Person> comparePersons = personList.Distinct(new PersonCompare()).ToList<Person>(); foreach (Person p in comparePersons) { Console.WriteLine("Name:{0} Age:{1}", p.Name, p.Age); } Console.ReadLine(); } } }
标签:Console,Name,Person,C#,Age,Linq,Distinct,new,public From: https://www.cnblogs.com/exesoft/p/16790688.html