Summary: in this tutorial, you’ll learn how to use the LINQ Count() method to return the number of elements in a sequence.
Introduction to the LINQ Count() method
The LINQ Count() is an extension method that returns the number of elements in a sequence or the number that represent how many elements that satisfy a condition.
int Count<TSource> (
this IEnumerable<TSource> source,
Func<TSource,bool> predicate
);Code language: C# (cs)In this syntax:
sourcein the input sequence with theIEnumerable<T>.predicateis a function to test each element for a specified condition.
The Count() function returns the number of elements or the number of elements that satisfy a condition.
LINQ Count() method examples
Let’s take some examples of using the LINQ Count() method.
1 ) Using the LINQ Count() method to count the number of elements in a sequence
The following program demonstrates how to use the Count() method to get the number of names whose lengths are less than or equal to 3:
using static System.Console;
var names = new string[] { "Alice", "Bob", "Peter", "David", "Ted" };
var shortNameCount = names.Count(name => name.Length <= 3);
WriteLine($"{shortNameCount} people have short names.");Code language: C# (cs)Output:
2 people have short names.Code language: C# (cs)2) Using the LINQ Count() method to count the elements that satisfy a condition
The following example uses the Count() method to get the number of employees in the IT department:
using System.Net.Http.Headers;
using static System.Console;
namespace LINQDemo
{
class Employee
{
public string? Name { get; set; }
public string? Department { get; set; }
public int Salary { get; set; }
}
class Program
{
public static void Main()
{
var employees = new List<Employee>() {
new() { Name = "John", Department = "HR", Salary = 50000 },
new() { Name = "Jane", Department = "IT", Salary = 60000 },
new() { Name = "Bob", Department = "HR", Salary = 45000 },
new() { Name = "Sara", Department = "IT", Salary = 55000 },
new() { Name = "Tom", Department = "IT", Salary = 65000 }
};
var result = employees.Count(e => e.Department == "IT");
WriteLine($"The IT employee count: {result}");
}
}
}Code language: C# (cs)Output:
The IT employee count: 3Code language: C# (cs)How it works.
First, define the Employee class with three properties Name, Department, and Salary:
class Employee
{
public string? Name { get; set; }
public string? Department { get; set; }
public int Salary { get; set; }
}Code language: C# (cs)Second, define a list of Employee objects and initialize it with five elements:
var employees = new List<Employee>()
{
new() { Name = "John", Department = "HR", Salary = 50000 },
new() { Name = "Jane", Department = "IT", Salary = 60000 },
new() { Name = "Bob", Department = "HR", Salary = 45000 },
new() { Name = "Sara", Department = "IT", Salary = 55000 },
new() { Name = "Tom", Department = "IT", Salary = 65000 }
};Code language: C# (cs)Third, count the number of employees in the IT department by using the Count() method:
var result = employees.Count(e => e.Department == "IT");Code language: C# (cs)The lambda expression e => e.Department == “” tests each Employee object and returns true if the department of the employee is IT.IT
Finally, write the number of IT employees to the console:
WriteLine($"The IT employee count: {result}");Code language: C# (cs)Summary
- Use the LINQ
Count()method to get the number of elements in a sequence that satisfy a specified condition.