LINQ AsEnumerable() Method

In LINQ, AsEnumerable() method is used to convert/cast specific types of given list items to its IEnumerable equivalent type.

Syntax of LINQ AsEnumerable method

Following is the syntax of using the LINQ AsEnumerable method to convert given list items to IEnumerable type.

LINQ AsEnumerable Syntax in C#

var result = numarray.AsEnumerable();

LINQ As Enumerable Syntax in VB.NET 

Dim result = numarray.AsEnumerable()

If you observe the above syntax, we are converting the “numarray” list/collection to an IEnumerable type.

Example of LINQ AsEnumerable() Method

Following is the example of using the 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 the above array, we convert the “numarray” list to IEnumerable type using the AsEnumerable() method.

Output of LINQ AsEnumerable() Method Example

Following is the result of the LINQ AsEnumerable() method example.

 

1
2
3
4

This is how we can use LINQ AsEnumerable() method to convert a list of items to an IEnumerable list.