Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ to DataSet, DataTable.AsEnumerable() not recognized

I am brand new to LINQ and am trying to query my DataSet with it. So I followed this example to the letter, and it does not work.

I know that my DataTable needs the .AsEnumerable on the end, but it is not recognized by the IDE. What am I doing wrong? Am I missing a reference/import that is not shown in the example (wouldn't be the first time a MSDN example was not quite right), and if so, which one? Or is it something else altogether?

Sample Code:

Imports System Imports System.Linq Imports System.Linq.Expressions Imports System.Collections.Generic Imports System.Data Imports System.Data.SqlClient Imports System.Data.Common Imports System.Globalization   //Fill the DataSet. Dim ds As New DataSet() ds.Locale = CultureInfo.InvariantCulture //See the FillDataSet method in the Loading Data Into a DataSet topic. FillDataSet(ds)  Dim products As DataTable = ds.Tables("Product")  Dim query = From product In products.AsEnumerable() _             Select product Console.WriteLine("Product Names:") For Each p In query     Console.WriteLine(p.Field(Of String)("Name")) Next 

The References in my project are:

System System.Data System.Drawing System.Windows.Forms System.Xml 
like image 407
GSTD Avatar asked Oct 16 '10 14:10

GSTD


People also ask

Can we use LINQ to query against a DataTable?

Can we use linq to query against a DataTable? Explanation: We cannot use query against the DataTable's Rows collection, since DataRowCollection doesn't implement IEnumerable<T>. We need to use the AsEnumerable() extension for DataTable.

What is DataTable AsEnumerable?

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.

How can you load data into a DataSet so that it can be queried using LINQ?

Data sources that implement the IEnumerable<T> generic interface can be queried through LINQ. Calling AsEnumerable on a DataTable returns an object which implements the generic IEnumerable<T> interface, which serves as the data source for LINQ to DataSet queries.

What is AsEnumerable?

AsEnumerable() in C# To cast a specific type to its IEnumerable equivalent, use the AsEnumerable() method. It is an extension method. The following is our array − int[] arr = new int[5]; arr[0] = 10; arr[1] = 20; arr[2] = 30; arr[3] = 40; arr[4] = 50; Now, get the IEnumerable equivalent.


1 Answers

While the class holding the extensions is in the System.Data namespace, it's located in an assembly that isn't added to your project by default. Add a reference to System.Data.DataSetExtensions to your project and it should be ok. Remember that, even after you've added the reference, any class that expects to use the extension methods defined in the class will need to have a using statement for System.Data as well.

like image 97
tvanfosson Avatar answered Sep 22 '22 18:09

tvanfosson