C# Enumerable LongCount(IEnumerable, Func)

Description

Returns an Int64 that represents how many elements in a sequence satisfy a condition.

Syntax


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

Parameters

  • TSource - The type of the elements of source.
  • source - An IEnumerable that contains the elements to be counted.
  • predicate - A function to test each element for a condition.

Returns

returns A number that represents how many elements in the sequence satisfy the condition in the predicate function.

Example

The following code example demonstrates how to use LongCount to count the elements in an array that satisfy a condition.


/*w w w.j  av a2 s .  c o  m*/
using System;
using System.Linq;
using System.Collections.Generic;
public class MainClass{
  public static void Main(String[] argv){  
    Pet[] pets = { new Pet { Name="a", Age=8 },
                   new Pet { Name="b", Age=4 },
                   new Pet { Name="c", Age=1 } };

    const int Age = 3;

    long count = pets.LongCount(pet => pet.Age > Age);

    Console.WriteLine("There are {0} animals over age {1}.", count, Age);
  }
}
    
class Pet{
   public string Name { get; set; }
   public int Age { get; set; }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Linq »




Enumerable