LINQ中的count()函数用于统计列表或集合中的元素数。
下面是定义LINQ Count函数以计算列表中的项数的语法。
C#中LINQ count()函数的语法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace ConsoleApp1 { class Program { static void Main(string[] args) { //create an array Num of type int int[] Num = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Console.WriteLine("Find the count of the elements:"); /to find the number of values in Num array will apply the count function on the array Num/ int Count = Num.Count(); /print the value of Count variable which is type of int with the helplp of WriteLine function/ Console.WriteLine("The Count is {0}", Count); Console.ReadLine(); } } }
Here, we used Count () function to find out the count of the given collection "Num".
当我们执行上面的LINQ count()示例时,我们将得到如下所示的结果:
