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
Return the total elements in a sequence as a 64-bit signed integer in C#
The LongCount() method in C# returns the total number of elements in a sequence as a 64-bit signed integer (long). This method is particularly useful when dealing with large datasets where the element count might exceed the range of a regular 32-bit integer.
The LongCount() method is available in both LINQ to Objects and LINQ to Entities, and can be used with any IEnumerable<T> or IQueryable<T> collection.
Syntax
Following is the syntax for using LongCount() method −
public static long LongCount<TSource>(this IEnumerable<TSource> source) public static long LongCount<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
Parameters
source − The sequence to count elements in.
predicate − (Optional) A function to test each element for a condition.
Return Value
Returns a long value representing the total number of elements in the sequence, or the number of elements that satisfy the condition if a predicate is provided.
Using LongCount() on Arrays
Here's how to count all elements in an array −
using System;
using System.Linq;
class Demo {
static void Main() {
string[] num = { "One", "Two", "Three", "Four", "Five" };
long res = num.LongCount();
Console.WriteLine("{0} elements", res);
}
}
The output of the above code is −
5 elements
Using LongCount() with Predicate
You can also count elements that meet a specific condition −
using System;
using System.Linq;
class Demo {
static void Main() {
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
long evenCount = numbers.LongCount(x => x % 2 == 0);
long greaterThanFive = numbers.LongCount(x => x > 5);
Console.WriteLine("Even numbers: {0}", evenCount);
Console.WriteLine("Numbers greater than 5: {0}", greaterThanFive);
}
}
The output of the above code is −
Even numbers: 5 Numbers greater than 5: 5
LongCount() vs Count()
| LongCount() | Count() |
|---|---|
Returns long (64-bit signed integer) |
Returns int (32-bit signed integer) |
| Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | Range: -2,147,483,648 to 2,147,483,647 |
| Use for large datasets | Use for smaller datasets |
| Slightly more memory overhead | More memory efficient for small counts |
Using LongCount() with Collections
using System;
using System.Collections.Generic;
using System.Linq;
class Demo {
static void Main() {
List<string> fruits = new List<string> { "Apple", "Banana", "Orange", "Grapes", "Mango" };
long totalFruits = fruits.LongCount();
long fruitsStartingWithA = fruits.LongCount(f => f.StartsWith("A"));
Console.WriteLine("Total fruits: {0}", totalFruits);
Console.WriteLine("Fruits starting with 'A': {0}", fruitsStartingWithA);
}
}
The output of the above code is −
Total fruits: 5 Fruits starting with 'A': 1
Conclusion
The LongCount() method is essential when working with large datasets where element counts might exceed the 32-bit integer limit. It provides the same functionality as Count() but returns a 64-bit long integer, making it suitable for big data scenarios and ensuring your applications can handle massive collections without overflow exceptions.
