Enumerable.Any<(Of <(TSource>)>) 方法

Any<(Of <(TSource>)>)(IEnumerable<(Of <(TSource>)>))

确定序列是否包含任何元素。

Any<(Of <(TSource>)>)(IEnumerable<(Of <(TSource>)>), Func<(Of <(TSource, Boolean>)>))

确定序列中的任何元素是否都满足条件。

类型参数

TSource

source 中的元素的类型。

参数

source

类型:System.Collections.Generic.IEnumerable<(Of <(TSource>)>)
一个 IEnumerable<(Of <(T>)>),其元素将应用谓词。

predicate

类型:System.Func<(Of <(TSource, Boolean>)>)
用于测试每个元素是否满足条件的函数。

返回值

类型:System.Boolean
如果源序列中的任何元素都通过指定谓词中的测试,则为 true;否则为 false

 

代码
    public static class LinqAny
    {
        
public static void Show(object obj)
        {
            HttpContext.Current.Response.Write(
"<BR>" + obj.ToString());
        }

        
public static void AnyDemo()
        {
            
int[] nums = new int[] { };
            
//确定序列是否包含任何元素
            bool tag = nums.Any();
            Show(
"result: " + tag);

            var strs 
= new[] { "one""two""three""four" };
            
//确定序列中是否有元素满足条件的
            tag = strs.Any<string>((s) => { LinqAggregate.Show("element: "+s); return s .IndexOf("u")>0; });
            Show(
"result: " + tag);
        }
    }
调用:
     
protected void Page_Load(object sender, EventArgs e)
    {
        LinqAny.AnyDemo();
    }
结果:
    result: False
    element: one
    element: two
    element: three
    element: four
    result: True 
 

 

 

posted on 2010-06-23 16:45  keely  阅读(500)  评论(0编辑  收藏  举报