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

本文介绍了Linq中Any方法的使用方式,包括如何检查集合是否包含元素及如何判断集合中的元素是否满足特定条件。提供了具体的代码示例并展示了运行结果。

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

 

ExpandedBlockStart.gif 代码
     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 
 

 

 

转载于:https://www.cnblogs.com/keely/archive/2010/06/23/1763749.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值