📜  LINQ |综合运营商|长期计数

📅  最后修改于: 2021-05-29 21:32:09             🧑  作者: Mango

在LINQ中,可以使用Count运算符对给定序列或集合的元素进行计数。但是count运算符有一个缺点,即它仅适用于MaxValue,即2147483647 ,如果尝试超过此值,则它将抛出OverflowException 。因此,在LongCount运算符克服了此缺点。它允许您计算大型集合或序列中存在的元素,仅自愿满足那些给定谓词函数的那些元素。换句话说,它返回一个Int64值,该值表示序列或集合中的元素数量。此方法以两种不同的方式重载:

  1. LongCount (IEnumerable ,Func ):此方法用于返回一个Int64值,该值表示序列中有多少个元素满足给定条件。
  2. LongCount (IEnumerable ):此方法用于返回一个Int64值,该值表示给定序列或集合中存在的元素总数。

重要事项:

  • 它不支持C#和VB.Net语言的查询语法。
  • 它可以支持C#和VB.Net语言的方法语法。
  • 它同时存在于Queryable和Enumerable类中。
  • 如果源或谓词为null,则将抛出ArgumentNullException。
  • 如果匹配元素的数量超过MaxValue,即9223372036854775807 ,它将抛出OverflowException。

范例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);
    }
}
输出:
5

范例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 emp = new List() {
  
            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);
    }
}
输出:
How many employee present in the company: 6