當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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() function



相關用法


注:本文由純淨天空篩選整理自 LINQ Count() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。