Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Linq Count method
The Count method in LINQ returns the number of elements in a sequence. It is one of the most commonly used LINQ methods for determining collection size and can be used with arrays, lists, and any IEnumerable collection.
Syntax
Following is the syntax for the basic Count method −
int count = collection.Count();
Following is the syntax for Count with a predicate condition −
int count = collection.Count(predicate);
Using Count() on Arrays and Collections
The Count method can be applied to any collection that implements IEnumerable. Here's how to count all elements in an array −
using System;
using System.Linq;
class Demo {
static void Main() {
string[] arr = { "Java", "C++", "Python"};
int arr_count = arr.Count();
Console.WriteLine("Count of arrays: {0}", arr_count);
// Using with List
var numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int numberCount = numbers.Count();
Console.WriteLine("Count of numbers: {0}", numberCount);
}
}
The output of the above code is −
Count of arrays: 3 Count of numbers: 10
Using Count() with Conditions
The Count method can accept a predicate to count only elements that meet specific criteria −
using System;
using System.Linq;
class Demo {
static void Main() {
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// Count even numbers
int evenCount = numbers.Count(x => x % 2 == 0);
Console.WriteLine("Even numbers count: {0}", evenCount);
// Count numbers greater than 5
int greaterThanFive = numbers.Count(x => x > 5);
Console.WriteLine("Numbers > 5 count: {0}", greaterThanFive);
// Count strings with specific length
string[] words = { "apple", "banana", "cherry", "date", "elderberry" };
int longWords = words.Count(w => w.Length > 5);
Console.WriteLine("Words with length > 5: {0}", longWords);
}
}
The output of the above code is −
Even numbers count: 5 Numbers > 5 count: 5 Words with length > 5: 3
Count vs Length vs Count Property
| Method/Property | Use Case | Performance |
|---|---|---|
| Array.Length | Arrays only | O(1) - fastest |
| List<T>.Count | Lists and collections | O(1) - fast |
| LINQ Count() | Any IEnumerable, with conditions | O(n) - iterates through collection |
Common Use Cases
Here are practical examples showing different scenarios where Count is useful −
using System;
using System.Linq;
class Demo {
static void Main() {
int[] scores = { 85, 92, 78, 96, 88, 76, 94, 82 };
// Count passing scores (>= 80)
int passingCount = scores.Count(score => score >= 80);
Console.WriteLine("Students passing (>= 80): {0}", passingCount);
// Count excellent scores (>= 90)
int excellentCount = scores.Count(score => score >= 90);
Console.WriteLine("Excellent scores (>= 90): {0}", excellentCount);
// Count total scores
int totalStudents = scores.Count();
Console.WriteLine("Total students: {0}", totalStudents);
// Calculate percentage
double passingPercentage = (double)passingCount / totalStudents * 100;
Console.WriteLine("Passing percentage: {0:F1}%", passingPercentage);
}
}
The output of the above code is −
Students passing (>= 80): 6 Excellent scores (>= 90): 3 Total students: 8 Passing percentage: 75.0%
Conclusion
The LINQ Count method is essential for determining collection sizes and counting elements that meet specific conditions. Use Count() for conditional counting with predicates, but prefer Length or Count properties for simple size checks on arrays and lists for better performance.
