📌  相关文章
📜  计算LINQ序列中存在的元素数量?

📅  最后修改于: 2021-05-29 18:44:07             🧑  作者: Mango

在LINQ中,您可以使用Count Method来计数给定序列中存在的元素总数。此方法返回给定序列中存在的元素总数。

可以通过两种不同的方式重载此方法:

  1. Count ():此方法返回给定指定序列中存在的元素总数。此方法的返回类型为System.Int32 。如果源为null,则此方法提供ArgumentNullException;如果源的值较大,则提供OverflowException。它不支持C#中的查询语法,但是可以将查询包装到方括号()中,并使用聚合函数,如示例1所示。它支持VB.NET中的查询语法。

    句法:

    int Count();
  2. Count (Func 谓词):此方法用于返回满足给定条件的项目数。此方法的返回类型为System.Int32 。如果源或谓词为null,则此方法提供ArgumentNullException;如果源或谓词的值较大,则提供OverflowException。

    句法:

    int Count(Func predicate);

    注意: VB.NET不支持带有谓词参数的此方法。

范例1:

// C# program to find total number of
// elements present in the given array
using System;
using System.Linq;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
  
        // Data source
        int[] sequence = {6, 455, 50, 56, 102,
                          89, 9, 100, 67, 29};
  
        // Display the sequence
        Console.WriteLine("The sequence is: ");
  
        foreach(int s in sequence)
        {
            Console.WriteLine(s);
        }
  
        // Finding the total number of elements
        // present in the given sequence
        // Using Count function
        int result = sequence.Count();
        Console.WriteLine("Total number of Elements: {0}", result);
    }
}
输出:
The sequence is: 
6
455
50
56
102
89
9
100
67
29
Total number of Elements: 10

范例2:

// C# program to count the total
// number of the 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 },
        };
  
        // Count the total number of employees
        // Using Count () method
        var res = (from e in emp
                       select e.emp_id)
                      .Count();
  
        Console.WriteLine("Total number of Employees: {0}", res);
    }
}
输出:
Total number of Employees: 6