zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    TypeWonder – 在任何网站上实时预览字体效果
    16例美丽的国外服务和应用程序网站设计欣赏
    25款响应式,支持视网膜显示的 Wordpress 主题
    扁平化设计的最新趋势 – 长阴影(Long Shadow)
    HTML5 Maker – 在线轻松制作 HTML5 动画效果
    经典设计:30个另类的 404 not found 页面设计
    字体大宝库:设计师必备的专业免费英文字体
    小伙伴们惊呆了!10行 JavaScript 实现文本编辑器
    Glyphish – 精心打造的 iPhone & iPad 应用程序图标
    精品资源:40个实用的 PSD 贴纸模板《下篇》
  • 原文地址:https://www.cnblogs.com/lanpingwang/p/6602969.html
Copyright © 2011-2022 走看看