• LINQ的Any方法


    返回布尔值,判断集合中是否有元素满足某一条件。

     

     
    source code:

    IEnumerable<string> str = new List<string> {
                    "asdf","fgsdfg","tyuiu","ryury","ituyitu"
                };
    
    
                if (str.Any(x => x.EndsWith("iu")))
                {
                    Write("true");
                }
                else
                {
                    Write("false");
                }
    View Code


    或者你可以如下这样写:

    source code:

    IEnumerable<string> str = new List<string> {
                    "asdf","fgsdfg","tyuiu","ryury","ituyitu"
                };
    
    
                //if (str.Any(x => x.EndsWith("iu")))
                //{
                //    Write("true");
                //}
                //else
                //{
                //    Write("false");
                //}
    
                bool blnResult = str.Any();
    
                blnResult = str.Any<string>((s) =>
                {
                    return s.LastIndexOf("iu") > 0;
                });
    
                if (blnResult)
                {
                    Write("true");
                }
                else
                {
                    Write("false");
                }
    View Code



  • 相关阅读:
    POJ 2486
    奇怪的电梯
    穿越泥地(mud)
    救援行动(save)
    As Fast As Possible
    Connecting Universities
    They Are Everywhere
    Cells Not Under Attack
    吃饭
    花店橱窗(flower)
  • 原文地址:https://www.cnblogs.com/insus/p/5383102.html
Copyright © 2020-2023  润新知