Count the Number of Element Present in the Sequence in LINQ?

Language Integrated Query (LINQ) is a powerful feature in C# that allows for efficient data manipulation. One common task when working with collections is determining the number of elements in a sequence. This article will guide you through using LINQ to count elements in various scenarios.

Understanding LINQ Sequences

A sequence in LINQ is any object that implements the IEnumerable or IEnumerable<T> interface. This includes arrays, lists, collections, and query results. LINQ provides several methods to count elements efficiently.

Syntax

Following is the syntax for basic counting operations

// Count all elements
int totalCount = sequence.Count();

// Count elements matching a condition
int conditionalCount = sequence.Count(predicate);

// Count elements (alternative for ICollection)
int collectionCount = sequence.Count;

// Get count as long (for very large sequences)
long longCount = sequence.LongCount();

Using Count() Method

The Count() extension method returns the number of elements in a sequence

Example

using System;
using System.Collections.Generic;
using System.Linq;

class Program {
   static void Main() {
      List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
      string[] names = { "Alice", "Bob", "Charlie", "David" };
      
      int numberCount = numbers.Count();
      int nameCount = names.Count();
      
      Console.WriteLine("Numbers count: " + numberCount);
      Console.WriteLine("Names count: " + nameCount);
   }
}

The output of the above code is

Numbers count: 10
Names count: 4

Counting with Conditions

The Count() method accepts a predicate function to count only elements that satisfy specific conditions

Example

using System;
using System.Collections.Generic;
using System.Linq;

class Program {
   static void Main() {
      List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
      
      int evenCount = numbers.Count(n => n % 2 == 0);
      int greaterThanFive = numbers.Count(n => n > 5);
      int singleDigits = numbers.Count(n => n < 10);
      
      Console.WriteLine("Even numbers: " + evenCount);
      Console.WriteLine("Numbers > 5: " + greaterThanFive);
      Console.WriteLine("Single digits: " + singleDigits);
   }
}

The output of the above code is

Even numbers: 5
Numbers > 5: 5
Single digits: 9

Counting with Complex Objects

You can count elements in collections of custom objects using properties and complex conditions

Example

using System;
using System.Collections.Generic;
using System.Linq;

class Employee {
   public string Name { get; set; }
   public int Age { get; set; }
   public string Department { get; set; }
}

class Program {
   static void Main() {
      List<Employee> employees = new List<Employee> {
         new Employee { Name = "John", Age = 25, Department = "IT" },
         new Employee { Name = "Sarah", Age = 30, Department = "HR" },
         new Employee { Name = "Mike", Age = 35, Department = "IT" },
         new Employee { Name = "Lisa", Age = 28, Department = "Finance" }
      };
      
      int totalEmployees = employees.Count();
      int itEmployees = employees.Count(e => e.Department == "IT");
      int youngEmployees = employees.Count(e => e.Age < 30);
      
      Console.WriteLine("Total employees: " + totalEmployees);
      Console.WriteLine("IT employees: " + itEmployees);
      Console.WriteLine("Employees under 30: " + youngEmployees);
   }
}

The output of the above code is

Total employees: 4
IT employees: 2
Employees under 30: 2

Performance Considerations

Method Best For Performance
Count property Collections like List<T> O(1) - Fastest
Count() method Any IEnumerable sequence O(n) - Enumerates all elements
Count(predicate) Conditional counting O(n) - Must check each element
LongCount() Very large sequences O(n) - Returns long instead of int

Using LongCount for Large Sequences

For sequences that might contain more than int.MaxValue elements, use LongCount()

Example

using System;
using System.Linq;

class Program {
   static void Main() {
      var largeSequence = Enumerable.Range(1, 1000000);
      
      long count = largeSequence.LongCount();
      long evenCount = largeSequence.LongCount(x => x % 2 == 0);
      
      Console.WriteLine("Total count: " + count);
      Console.WriteLine("Even numbers: " + evenCount);
   }
}

The output of the above code is

Total count: 1000000
Even numbers: 500000

Conclusion

LINQ provides flexible methods to count elements in sequences, from simple total counts to complex conditional counting. The Count() method works with any IEnumerable source and supports predicates for filtering, while LongCount() handles very large datasets efficiently.

Updated on: 2026-03-17T07:04:36+05:30

487 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements