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


LINQ AsEnumrable()用法及代碼示例

在 LINQ 中,AsEnumerble() 方法用於將給定列表的特定類型轉換為其 IEnumerable() 等效類型。

LINQ AsEnumerable() 方法的語法

C# 代碼

var result = numarray.AsEnumerable();

在上麵的語法中,我們將 "numarray" 的列表轉換為 IEnumerable 類型。

LINQ AsEnumerable() 方法示例

下麵是使用 LINQ AsEnumerable 方法將列表轉換為 IEnumerable 的示例。

using System;
using System. Collections;
using System.Collections.Generic;
using System. Linq;
using System. Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
    class Program1
    {
        static void Main(string[] args)
        {
//here we are creating an array NumArray type of int
            int[] NumArray = new int[] { 1, 2, 3, 4,5};
//After applying the AsEnumerable method the output will be store in variable result
            var result = NumArray.AsEnumerable();
//Now we will print the value of variable result one by one with the help of foreach loop
            foreach (var number in result)
        {
            Console.WriteLine(number);
        }
            Console.ReadLine();
        }
        }
}

在上麵的示例中,我們使用 AsEnumerable 方法將 "NumArray" 的列表轉換為 IEnumerable 類型。

輸出:

LINQ AsEnumrable() Method



相關用法


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