首页 > 编程语言 >[C#教学备课]Linq的Distinct功能验证

[C#教学备课]Linq的Distinct功能验证

时间:2022-10-14 10:12:53浏览次数:42  
标签:Console Name Person C# Age Linq Distinct new public

代码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

相关文章

  • leetcode每日一题:940.不同的子序列Ⅱ
    题目描述给定一个字符串s,计算s的不同非空子序列的个数。因为结果可能很大,所以返回答案需要对10^9+7取余。字符串的子序列是经由原字符串删除一些(也可能不删除)字......
  • 看一遍就理解:MVCC原理详解
    前言MVCC实现原理是一道非常高频的面试题,最近技术讨论群的小伙伴一直在讨论,趁着国庆节有空,我们一起来聊聊。1.相关数据库知识点回顾1.1什么是数据库事务,为什么要有事务......
  • leetcode必备算法:聊聊滑动窗口
    前言我们刷leetcode的时候,经常会遇到滑动窗口类型题目。滑动窗口问题非常经典,也很有技巧性,一般大厂也喜欢问。今天跟大家一起来学习滑动窗口的套路,文章如果有不正确的地方,......
  • 异步编程利器:CompletableFuture详解
    前言最近刚好使用CompeletableFuture优化了项目中的代码,所以跟大家一起学习CompletableFuture。公众号:捡田螺的小男孩一个例子回顾Future因为CompletableFuture实现了​​......
  • C语言操作符大全和详解(上)
    ......
  • layui 下拉菜单被父级元素overflow-y:scroll遮挡显示不全
        提供一种解决思路,将下拉菜单的position设为fixed<formid="form"class="layui-form"><divstyle="overflow-y:scroll;height:200px;width:300px;......
  • LeetCode 1116. Print Zero Even Odd
    原题链接在这里:https://leetcode.com/problems/print-zero-even-odd/题目:Youhaveafunction printNumber thatcanbecalledwithanintegerparameterandprints......
  • C#更新packages
    有时重新clone代码到本地时,会出现references中引用的包报错,这时可以重新更新下载这些包:在VS工具栏中,Tools→NuGetPackageManager→PackageManagerConsole输入Update-......
  • React组件之间的通信方式总结(下)
    一、写一个时钟用react写一个每秒都可以更新一次的时钟importReactfrom'react'importReactDOMfrom'react-dom'functiontick(){letele=<h1>{new......
  • python运用Anaconda3设置开机启动项目
    1、安装Anaconda3-5.2.0-Windows-x86_64.exe2、利用Anaconda创建运行环境py38 3、在Anaconda3-5.2.0-Windows-x86_64.exe的安装目录下C:\ProgramData\Anaconda3\Scripts......