zoukankan      html  css  js  c++  java
  • LINQ中限定操作符(十一)

    限定操作符运算返回一个Boolean值,该值指示序列中是否有一些元素满足条件或者是否所有元素都满足条件

    一、All操作符

    All方法用来确定是否序列中的所有元素都满足条件

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace LimitOperation
    {
        class Program
        {
            static void Main(string[] args)
            {
                string[] source1 = new string[] { "A", "B", "C", "D", "E", "F" };
    
                string[] source2 = new string[] { "A", "A", "A", "A", "A", "A" };
    
                Console.WriteLine(source1.All(w => w == "A")); //输出"False"
    
                Console.WriteLine(source2.All(w => w == "A")); //输出 "True"
    
                Console.ReadKey();
            }
        }
    }
    View Code

    二、Any操作符

    Any方法的无参方式用来确定序列是否包含任何元素。

    Any方法的有参方式用来确定序列中是否有元素满足条件。只要有一个元素符合指定条件即返回true,如果一个符合指定条件的元素都没有则返回false

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace LimitOperation
    {
        class Program
        {
            static void Main(string[] args)
            {
                string[] source1 = new string[] { "A", "B", "C", "D", "E", "F" };
                string[] source2 = new string[] { "A", "A", "A", "A", "A", "A" };
                Console.WriteLine(source1.Any()); // 输出"True"
    
                Console.WriteLine(source1.Any(w => w == "A")); //输出 "True"
    
                Console.WriteLine(source2.Any(w => w == "G")); //输出 "False"
    
                Console.ReadKey();
            }
        }
    }
    View Code

    三、Contains操作符

     Contains方法用来确定序列是否包含满足指定条件的元素。如果有返回true,否则返回false

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace LimitOperation
    {
        class Program
        {
            static void Main(string[] args)
            {
                string[] source1 = new string[] { "A", "B", "C", "D", "E", "F" };
    
                Console.WriteLine(source1.Contains("A")); //输出 "True"
    
                Console.WriteLine(source1.Contains("G")); //输出 "False"
    
                Console.ReadKey();
            }
        }
    }
    View Code

    Contains还有另外一个重载的方法,该重载方法的参数是一个实现IEqualityComparer<TSource>接口的类型

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace LimitOperation
    {
        /// <summary>
        /// EqualityComparerEquals类实现IEqualityComparer接口
        /// </summary>
        public class EqualityComparerEquals : IEqualityComparer<string>
        {
            public bool Equals(string x, string y)
            {
                return x == y;
            }
            public int GetHashCode(string obj)
            {
                return obj.ToString().GetHashCode();
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                string[] source1 = new string[] { "A", "B", "C", "D", "E", "F" };
    
                var comparer = source1.Contains("F", new EqualityComparerEquals());
                Console.WriteLine(comparer); //输出"True"
                Console.ReadKey();
            }
        }
    }
    View Code
  • 相关阅读:
    警示
    【拒绝挂分】盘点蒟蒻ghy的各种sb错误
    牛客NOIPtg day5 B-demo的gcd
    数字校园APP——视频分享
    数字校园APP——软件需求规格说明书
    数字校园APP——可行性报告分析
    数字校园APP开发与应用
    结对编程第二次作业——四则运算自动生成器
    软件工程第四次作业
    软件工程第三次作业
  • 原文地址:https://www.cnblogs.com/LuckyZLi/p/12716772.html
Copyright © 2011-2022 走看看