Open In App

LINQ | Aggregate Operators | LongCount

Last Updated : 06 Jun, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

In LINQ, a Count operator is available to count the elements of the given sequence or collection. But there is a drawback of count operator, i.e, it only works for the MaxValue, i.e, 2147483647, if you try to more than this value then it will throw an OverflowException. So this drawback is overcome in the LongCount operator. It allows you to count the elements present in the large collection or sequence, voluntary only those elements that satisfy the given predicate function. Or in other words, it returns an Int64 value that represents the number of elements in a sequence or collection. This method is overloaded in two different ways:

  1. LongCount<TSource>(IEnumerable<TSource>, Func<TSource, Boolean>): This method is used to return an Int64 value that represents how many elements in a sequence satisfy the given condition.
  2. LongCount<TSource>(IEnumerable<TSource>): This method is used to return an Int64 value that represents the total number of elements present in the given sequence or collection.

Important Points:

  • It does not support query syntax in C# and VB.Net languages.
  • It can support method syntax in C# and VB.Net languages.
  • It present in both the Queryable and Enumerable class.
  • It will throw an ArgumentNullException, if the source or predicate is null.
  • It will throw an OverflowException, if the number of matching elements exceeds MaxValue, i.e, 9223372036854775807.

Example 1:




// C# program to illustrate the
// use of LongCount method
using System;
using System.Linq;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
  
        // Data source
        string[] sequence = {"Geeks", "gfg", "GeeksforGeeks"
                               "GFG", "C#", "Java", "Python"
                                   "WelcomeToGeeksforGeeks"};
  
        // Count how much elements present in
        // the sequence using LongCount function
        long res = sequence.LongCount(seq => seq.Length > 3);
        Console.WriteLine(res);
    }
}


Output:

5

Example 2:




// C# program to find the total
// number of employees
using System;
using System.Linq;
using System.Collections.Generic;
  
// Employee details
public class Employee {
  
    public int emp_id
    {
        get;
        set;
    }
  
    public string emp_name
    {
        get;
        set;
    }
  
    public string emp_gender
    {
        get;
        set;
    }
  
    public string emp_hire_date
    {
        get;
        set;
    }
  
    public int emp_salary
    {
        get;
        set;
    }
}
  
class GFG {
  
    // Main method
    static public void Main()
    {
        List<Employee> emp = new List<Employee>() {
  
            new Employee() {emp_id = 209, emp_name = "Anjita", emp_gender = "Female",
                                      emp_hire_date = "12/3/2017", emp_salary = 20000},
  
            new Employee() {emp_id = 210, emp_name = "Soniya", emp_gender = "Female",
                                    emp_hire_date = "22/4/2018", emp_salary = 30000},
  
            new Employee() {emp_id = 211, emp_name = "Rohit", emp_gender = "Male",
                                  emp_hire_date = "3/5/2016", emp_salary = 40000},
  
            new Employee() {emp_id = 212, emp_name = "Supriya", emp_gender = "Female",
                                      emp_hire_date = "4/8/2017", emp_salary = 40000},
  
            new Employee() {emp_id = 213, emp_name = "Anil", emp_gender = "Male",
                                emp_hire_date = "12/1/2016", emp_salary = 40000},
  
            new Employee() {emp_id = 214, emp_name = "Anju", emp_gender = "Female",
                                  emp_hire_date = "17/6/2015", emp_salary = 50000},
        };
  
        // Query to find the total
        // number of employees
        // using LongCount method
        long res = emp.Select(e => e.emp_id).LongCount();
          
        Console.WriteLine("How many employee present"+
                         " in the company: {0}", res);
    }
}


Output:

How many employee present in the company: 6


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads