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

 
LINQ的Any方法_Any

 
source code:

LINQ的Any方法_Any_02LINQ的Any方法_Any_03
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


或者你可以如下这样写:
LINQ的Any方法_EndsWith_04

 

source code:

LINQ的Any方法_Any_02LINQ的Any方法_Any_03
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