个性化阅读
专注于IT技术分析

LINQ Count()函数

点击下载

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();
        }
    }
}

在这里, 我们使用Count()函数找出给定集合” Num”的计数。

当我们执行上面的LINQ Count()示例时, 我们将得到如下所示的结果:

LINQ Count()函数

赞(0)
未经允许不得转载:srcmini » LINQ Count()函数

评论 抢沙发

评论前必须登录!