行易软件

少谈技术,多谈应用

博客园 首页 新随笔 联系 订阅 管理

代码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(); } } }

 

posted on 2022-10-14 10:00  行易软件技术团队  阅读(7)  评论(0编辑  收藏  举报