LINQ之Count,LongCount

返回LINQ大全首页

Count()

使用Count()可以获取序列的长度,代替LengthCount
public static int Count<TSource>( this IEnumerable<TSource> source );
MSDN

代码示例:

public static class Program
{
    static void Main( string[] args )
    {
        int[]           intArray    = new int[] { 1, 5, 8, 12, 15, 16 };
        List<string>    stringList  = new List<string>() { "正一郎", "清次郎", "誠三郎", "征史郎" };
        
        int intCount    = intArray.Count();
        int stringCount = stringList.Count();

        System.Console.WriteLine( "intArray:{0}", intCount );
        System.Console.WriteLine( "stringList:{0}", stringCount );

        System.Console.ReadKey();
    }
} 

intArray:6
stringList:4

不仅如此,还可以获取符合条件的数量。
public static int Count<TSource>( this IEnumerable<TSource> source, Func<TSource, bool> predicate );
MSDN

代码示例:

public static class Program
{
    static void Main( string[] args )
    {
        int[]           intArray    = new int[] { 1, 5, 8, 12, 15, 16 };
        List<string>    stringList  = new List<string>() { "正一郎", "清次郎", "誠三郎", "征史郎" };

        int intCount    = intArray.Count( value => value % 2 == 0 );
        int stringCount = stringList.Count( value => value.IndexOf( "三" ) >= 0 );

        System.Console.WriteLine( "intArray:{0}", intCount );
        System.Console.WriteLine( "stringList:{0}", stringCount );

        System.Console.ReadKey();
    }
}

intArray:3
stringList:1

LongCount()

Count()的return类型是int类型,int类型最大范围在20几亿,那么当我们的数据超过这个数量呢?

代码示例:

public static class Program
{
    static void Main( string[] args )
    {
    	// Enumerable.Range可以生成指定范围内的整数序列
        IEnumerable<int> intSequenceA   = Enumerable.Range( 0, int.MaxValue );
        IEnumerable<int> intSequenceB   = Enumerable.Range( int.MinValue, int.MaxValue );

        IEnumerable<int> intSequenceAB  = intSequenceA.Concat( intSequenceB );

        int count = 0;
        try
        {
            count   = intSequenceAB.Count();
        }
        catch( System.Exception i_exception )
        {
            System.Console.WriteLine( "异常:{0}", i_exception );

            System.Console.ReadKey();
            return;
        }

        System.Console.WriteLine( "intSequenceAB:{0}", count );
        
        System.Console.ReadKey();
    }

}

异常:System.OverflowException

结果发生了异常。遇到这种数据量特别庞大的情况,我们就可以用LongCount()代替,LongCount()的返回值为long
public static int Count<TSource>( this IEnumerable<TSource> source, Func<TSource, bool> predicate );
MSDN
让我们使用此方法重写发生异常的过程。

代码示例:

public static class Program
{
    static void Main( string[] args )
    {
        IEnumerable<int> intSequenceA   = Enumerable.Range( 0, int.MaxValue );
        IEnumerable<int> intSequenceB   = Enumerable.Range( int.MinValue, int.MaxValue );

        IEnumerable<int> intSequenceAB  = intSequenceA.Concat( intSequenceB );

        long count = 0;
        try
        {
            count   = intSequenceAB.LongCount();
        }
        catch( System.Exception i_exception )
        {
            System.Console.WriteLine( "异常:{0}", i_exception );

            System.Console.ReadKey();
            return;
        }

        System.Console.WriteLine( "intSequenceAB:{0}", count );

        System.Console.ReadKey();
    }

}

intSequenceAB:4294967294

这次没有异常,能够正确的得出结果。
LongCount()和Count()一样可以添加元素计数的条件。

public static long LongCount<TSource>( this IEnumerable<TSource> source, Func<TSource, bool> predicate );
MSDN

代码示例:

public static class Program
{
    static void Main( string[] args )
    {
        IEnumerable<int> intSequenceA   = Enumerable.Range( 0, int.MaxValue );
        IEnumerable<int> intSequenceB   = Enumerable.Range( int.MinValue, int.MaxValue );

        IEnumerable<int> intSequenceAB  = intSequenceA.Concat( intSequenceB );

        long count = 0;
        try
        {
            count   = intSequenceAB.LongCount( value => value > -1000000 );
        }
        catch( System.Exception i_exception )
        {
            System.Console.WriteLine( "异常:{0}", i_exception );

            System.Console.ReadKey();
            return;
        }

        System.Console.WriteLine( "intSequenceAB:{0}", count );

        System.Console.ReadKey();
    }
}

intSequenceAB:2148483645
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我寄人间雪满头丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值