zoukankan      html  css  js  c++  java
  • 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();

  • 相关阅读:
    springmvc的执行流程
    深入理解设计模式(五):抽象工厂模式
    深入理解设计模式(四):工厂方法模式
    深入理解设计模式(三):策略模式
    写给三十岁的自己
    asp.net引用System.Speech实现语音提示
    深入理解设计模式(序):常用的7大设计原则
    深入理解设计模式(二):简单工厂模式
    深入理解设计模式(一):单例模式
    解决基于IIS的.net core HttpWebRequest 连接特别慢
  • 原文地址:https://www.cnblogs.com/refuge/p/8150650.html
Copyright © 2011-2022 走看看