• 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
  • 相关阅读:
    云HBase发布全文索引服务,轻松应对复杂查询
    eclipse调试断点
    IntelliJ IDEA使用小技巧
    jQuery遍历not的用法
    request.querystring和request.form的区别
    ppt打不开,显示发现文件中的内容有问题。可尝试修复此演示文稿
    中文乱码 encodeURI来解决URL传递时的中文问题
    Http请求中Content-Type讲解以及在Spring MVC中的应用
    Display Tag Lib Table进行分页
    js刷新页面
  • 原文地址:https://www.cnblogs.com/LuckyZLi/p/12716772.html
Copyright © 2020-2023  润新知