In LINQ, AsEnumerable() method is used to convert/cast specific types of given list items to its IEnumerable equivalent type.
Following is the syntax of using LINQ AsEnumerable method to convert give list items to IEnumerable type.
C# Code
var result = numarray.AsEnumerable();
VB.NET Code
If you observe above syntax we are converting “numarray” list / collection to IEnumerable type.
Following is the example of using LINQ AsEnumerable method to convert the list of items to IEnumerable type.
C# Code
using System;
using System.Linq;
namespace LINQExamples
{
class Program
{
static void Main(string[] args)
{
int[] numarray = new int[] { 1, 2, 3, 4 };
var result = numarray.AsEnumerable();
foreach (var number in result)
{
Console.WriteLine(number);
}
Console.ReadLine();
}
}
}
VB.NET Code
Module Module1
Sub Main()
Dim numarray As Integer() = New Integer() {1, 2, 3, 4}
Dim result = numarray.AsEnumerable()
For Each number In result
Console.WriteLine(number)
Next
Console.ReadLine()
End Sub
End Module
If you observe above array we are converting “numarray” list to IEnumerable type by using AsEnumerable() method.
Following is the result of LINQ AsEnumerable() method example.
1
2
3
4
This is how we can use LINQ AsEnumerable() method to convert list of items to IEnumerable list.