C# Queryable LongCount Method

The LongCount() method in C# is part of the System.Linq namespace and returns the number of elements in a sequence as a long data type. This method is particularly useful when dealing with large collections where the count might exceed the range of an int (2,147,483,647).

The LongCount()IQueryable<T> and can be used with or without a predicate condition to filter elements before counting.

Syntax

Following is the syntax for the LongCount() method without a condition −

public static long LongCount<TSource>(this IQueryable<TSource> source)

Following is the syntax for the LongCount() method with a predicate condition −

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

Parameters

  • source − The IQueryable<T> sequence to count elements from.

  • predicate − (Optional) A function to test each element for a condition.

Return Value

Returns a long value representing the number of elements in the source sequence, or the number of elements that satisfy the condition if a predicate is provided.

Using LongCount() Without Predicate

Example

using System;
using System.Linq;

class Demo {
    static void Main() {
        string[] emp = { "Jack", "Mark", "Alice", "Bob" };
        long res = emp.AsQueryable().LongCount();
        Console.WriteLine("{0} employees in the Department", res);
        
        int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        long count = numbers.AsQueryable().LongCount();
        Console.WriteLine("Total numbers: {0}", count);
    }
}

The output of the above code is −

4 employees in the Department
Total numbers: 10

Using LongCount() With Predicate

Example

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.AsQueryable().LongCount(x => x % 2 == 0);
        Console.WriteLine("Even numbers: {0}", evenCount);
        
        long greaterThanFive = numbers.AsQueryable().LongCount(x => x > 5);
        Console.WriteLine("Numbers greater than 5: {0}", greaterThanFive);
        
        string[] names = { "Alice", "Bob", "Charlie", "David", "Emma" };
        long longNames = names.AsQueryable().LongCount(name => name.Length > 4);
        Console.WriteLine("Names with more than 4 characters: {0}", longNames);
    }
}

The output of the above code is −

Even numbers: 5
Numbers greater than 5: 5
Names with more than 4 characters: 2

LongCount() vs Count() Comparison

LongCount() Count()
Returns long data type Returns int data type
Range: 0 to 9,223,372,036,854,775,807 Range: 0 to 2,147,483,647
Better for very large collections Sufficient for most collections
Slightly more memory usage More memory efficient

Working with Large Collections

Example

using System;
using System.Linq;

class Demo {
    static void Main() {
        // Simulating a large collection
        var largeCollection = Enumerable.Range(1, 1000000).AsQueryable();
        
        long totalCount = largeCollection.LongCount();
        Console.WriteLine("Total elements: {0}", totalCount);
        
        long evenCount = largeCollection.LongCount(x => x % 2 == 0);
        Console.WriteLine("Even elements: {0}", evenCount);
        
        long divisibleByHundred = largeCollection.LongCount(x => x % 100 == 0);
        Console.WriteLine("Divisible by 100: {0}", divisibleByHundred);
    }
}

The output of the above code is −

Total elements: 1000000
Even elements: 500000
Divisible by 100: 10000

Conclusion

The LongCount() method is essential when working with large datasets where the element count might exceed the int range. It provides both parameterless and predicate-based counting, making it versatile for various counting scenarios in LINQ queries.

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

230 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements