• Linq 对象的比较 Contains,Max


     

    Contains

        public class StudentEquality : IEqualityComparer<Student>
        {
    
            public static StudentEquality Instance { get; set; }
    
            private StudentEquality()
            {
    
            }
    
            static StudentEquality()
            {
                Instance = new StudentEquality();
            }
    
    
            public bool Equals(Student x, Student y)
            {
                if (x == null || y == null)
                {
                    return false;
                }
    
                if (x.StudentID == y.StudentID)
                {
                    return true;
                }
    
                return false;
            }
    
            public int GetHashCode(Student obj)
            {
                return obj.GetHashCode();
            }
        }
                List<Student> studentList = new List<Student>() {
                    new Student() { StudentID = 1, StudentName = "Zohn", StandardID =1 },
                    new Student() { StudentID = 2, StudentName = "Moin", StandardID =1 },
                    new Student() { StudentID = 3, StudentName = "Bill", StandardID =2 },
                    new Student() { StudentID = 4, StudentName = "Ram1" , StandardID =2 },
                    new Student() { StudentID = 5, StudentName = "Ron1123", StandardID = 3  }
                };
    
                Student s = new Student() { StudentID = 5, StudentName = "Ron1123", StandardID = 3 };
                bool res = studentList.Contains(s, StudentEquality.Instance);
                Console.WriteLine("Contains = " + res);// True

     

     

    Max

        public class StudentComparable : IComparable<StudentComparable>
        {
            public Student Student { get; set; }
    
            public int CompareTo(StudentComparable other)
            {
                if (Student.StudentName.Length > other.Student.StudentName.Length)
                {
                    return 1;
                }
    
                if (Student.StudentName.Length == other.Student.StudentName.Length)
                {
                    return 0;
                }
                return -1;
            }
        }
                IList<Student> studentList = new List<Student>() {
                    new Student() { StudentID = 1, StudentName = "Zohn", StandardID =1 },
                    new Student() { StudentID = 2, StudentName = "Moin", StandardID =1 },
                    new Student() { StudentID = 3, StudentName = "Bill", StandardID =2 },
                    new Student() { StudentID = 4, StudentName = "Ram1" , StandardID =2 },
                    new Student() { StudentID = 5, StudentName = "Ron1123", StandardID = 3  }
                };
                //选出最长的名字的 Student
                StudentComparable max = studentList.Max(m => new StudentComparable { Student = m });
                Console.WriteLine(JsonConvert.SerializeObject(max.Student));
                Console.Read();

  • 相关阅读:
    pod hook 钩子函数 生命周期 lifecycle/postStart/preStop
    yaml基本语法 && 静态pod && pod生命周期 && pod健康检测
    netsh winsock reset
    H3C Password Contrpol
    H3C ssh 配置参考
    win7怎么修复winsock被删除
    pod健康检测 exec/http/tcpSocket
    win10 1709 新装 鼠标卡顿
    shell 标准输入输出及错误输出 重定向
    IP安全策略只允许指定IP连接远程桌面,限制IP登
  • 原文地址:https://www.cnblogs.com/refuge/p/8150650.html
Copyright © 2020-2023  润新知