• LINQ 学习路程 -- 查询操作 Quantifier Operators All Any Contain


    OperatorDescription
    All 判断所有的元素是否满足条件
    Any 判断存在一个元素满足条件
    Contain 判断是否包含元素
    IList<Student> studentList = new List<Student>() { 
            new Student() { StudentID = 1, StudentName = "John", Age = 18 } ,
            new Student() { StudentID = 2, StudentName = "Steve",  Age = 15 } ,
            new Student() { StudentID = 3, StudentName = "Bill",  Age = 25 } ,
            new Student() { StudentID = 4, StudentName = "Ram" , Age = 20 } ,
            new Student() { StudentID = 5, StudentName = "Ron" , Age = 19 } 
        };
    
    // checks whether all the students are teenagers    
    bool areAllStudentsTeenAger = studentList.All(s => s.Age > 12 && s.Age < 20);
    
    Console.WriteLine(areAllStudentsTeenAger);
    bool isAnyStudentTeenAger = studentList.Any(s => s.age > 12 && s.age < 20);

    Contains

    public static bool Contains<TSource>(this IEnumerable<TSource> source, TSource value);
    
    public static bool Contains<TSource>(this IEnumerable<TSource> source, 
                                         TSource value, 
                                         IEqualityComparer<TSource> comparer);
    IList<Student> studentList = new List<Student>() { 
            new Student() { StudentID = 1, StudentName = "John", Age = 18 } ,
            new Student() { StudentID = 2, StudentName = "Steve",  Age = 15 } ,
            new Student() { StudentID = 3, StudentName = "Bill",  Age = 25 } ,
            new Student() { StudentID = 4, StudentName = "Ram" , Age = 20 } ,
            new Student() { StudentID = 5, StudentName = "Ron" , Age = 19 } 
        };
    
    Student std = new Student(){ StudentID =3, StudentName = "Bill"};
    bool result = studentList.Contains(std); //returns false

    上面的result将是false,即使“”Bill"在集合中,因为Contains仅仅比较对象的引用,而不是对象的值。所以要比较对象的值,需要实现IEqualityComparer接口

    class StudentComparer : IEqualityComparer<Student>
    {
            public bool Equals(Student x, Student y)
            {
                if (x.StudentID == y.StudentID && 
                            x.StudentName.ToLower() == y.StudentName.ToLower())
                    return true;
    
                return false;
            }
    
            public int GetHashCode(Student obj)
            {
                return obj.GetHashCode();
            }
    }
    IList<Student> studentList = new List<Student>() { 
            new Student() { StudentID = 1, StudentName = "John", Age = 18 } ,
            new Student() { StudentID = 2, StudentName = "Steve",  Age = 15 } ,
            new Student() { StudentID = 3, StudentName = "Bill",  Age = 25 } ,
            new Student() { StudentID = 4, StudentName = "Ram" , Age = 20 } ,
            new Student() { StudentID = 5, StudentName = "Ron" , Age = 19 } 
        };
    
    Student std = new Student(){ StudentID =3, StudentName = "Bill"};
    bool result = studentList.Contains(std, new StudentComparer()); //returns true
  • 相关阅读:
    Unity3D命令行Build
    关于Unity3D的协程(Coroutine)
    牛腩购物34:支付宝的集成和使用(web网站和web应用程序一眼判断的区别),支付宝即时到账
    linqpad 的下载使用
    linq 的延时执行和即时执行
    冒泡排序
    c# 的继承,继承类实例化的时候,会先调用基类的构造函数,然后再调用自己的构造函数,base 的用法,有override和没有override的区别
    zoj1002 Fire Net
    zjut1675 I like DPS!!!
    zjut1673搭数字II
  • 原文地址:https://www.cnblogs.com/lanpingwang/p/6602969.html
Copyright © 2020-2023  润新知