Skip to main content
.NET Framework Class Library
DataTableExtensions..::.AsEnumerable Method

Returns an IEnumerable<(Of <(T>)>) object, where the generic parameter T is DataRow. This object can be used in a LINQ expression or method query.

Namespace: System.Data
Assembly: System.Data.DataSetExtensions (in System.Data.DataSetExtensions.dll)
Syntax
<ExtensionAttribute> _
Public Shared Function AsEnumerable ( _
	source As DataTable _
) As EnumerableRowCollection(Of DataRow)
public static EnumerableRowCollection<DataRow> AsEnumerable(
	this DataTable source
)
[ExtensionAttribute]
public:
static EnumerableRowCollection<DataRow^>^ AsEnumerable(
	DataTable^ source
)
static member AsEnumerable : 
        source:DataTable -> EnumerableRowCollection<DataRow> 

Parameters

source
Type: System.Data..::.DataTable
The source DataTable to make enumerable.

Return Value

Type: System.Data..::.EnumerableRowCollection<(Of <(DataRow>)>)
An IEnumerable<(Of <(T>)>) object, where the generic parameter T is DataRow.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type DataTable. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).
Exceptions
ExceptionCondition
ArgumentNullException

The source DataTable is nullNothingnullptra null reference (Nothing in Visual Basic).

Remarks

Language-Integrated Query (LINQ) queries work on data sources that implement the IEnumerable<(Of <(T>)>) interface or the IQueryable interface. The DataTable class does not implement either interface, so you must call the AsEnumerable method to use the DataTable as a source in the From clause of a LINQ query. You can also obtain custom, domain-specific operators, such as CopyToDataTable, by returning an IEnumerable<(Of <(T>)>) object. 

The enumerable object returned by the AsEnumerable method is permanently bound to the DataTable that produced it. Multiple calls to the AsEnumerable method will return multiple, independent queryable objects that are all bound to the source DataTable.

Examples

In the following sample, the DisplayProducts method receives a DataTable that contains a DataColumn named ProductName, extracts the ProductName values and then prints the values.


Imports System.Console
Module Module1
   Public Sub DisplayProducts(ByVal table As DataTable)
      Dim productNames = From products In table.AsEnumerable() Select products("ProductName")
      WriteLine("Product Names: ")
      For Each productName In productNames
         WriteLine(productName)
       Next
   End Sub

   Sub Main()
      Dim table As DataTable = New DataTable()
      table.Columns.Add("ID")
      table.Columns.Add("ProductName")

      table.Rows.Add("1", "Chai")
      table.Rows.Add("2", "Queso Cabrales")
      table.Rows.Add("3", "Tofu")

      DisplayProducts(table)
   End Sub
End Module


using System;
using System.Data;

class Program {
   public void DisplayProducts(DataTable table) {
      var productNames = from products in table.AsEnumerable() select products.Field<string>("ProductName");
      Console.WriteLine("Product Names: ");
      foreach (string productName in productNames) {
         Console.WriteLine(productName);
      }
   }

   static void Main(string[] args) {
      DataTable table = new DataTable();
      table.Columns.Add("ID");
      table.Columns.Add("ProductName");

      table.Rows.Add("1", "Chai");
      table.Rows.Add("2", "Queso Cabrales");
      table.Rows.Add("3", "Tofu");

      Program inst = new Program();
      inst.DisplayProducts(table);
   }
}

Version Information

.NET Framework

Supported in: 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.