43

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
Usman
  • 3,200
  • 3
  • 28
  • 47
GSTD
  • 1,064
  • 1
  • 12
  • 18

3 Answers3

113

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.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
tvanfosson
  • 524,688
  • 99
  • 697
  • 795
  • Thanks, that works now! I've been tearing out my hair on this one. :) +1 to everyone for the System.Data.DataSetExtensions, but you get the 'accept' for the much better explanation. Thanks again. – GSTD Oct 16 '10 at 14:52
  • Why is still there's a need to add a using statement even if it is already included in the references of the project? – Charmie Jan 07 '15 at 08:11
  • @Charmie the `using` statement makes the namespace available in the file/class. Because it's an extension method you can't reference it using the full namespace (when used as an extension) and so have to have the namespace included. – tvanfosson Jan 07 '15 at 13:50
  • 2
    I've added the assembly reference to `System.Data.DataSetExtensions` (C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Data.DataSetExtensions.dll) and have the necessary `using System.Data` and `using System.Data.DataSetExtensions`, but I'm getting the error that "The type or namespace name 'DataSetExtensions' does not exist in the namespace 'System.Data' (are you missing an assembly reference?) " – Tim May 09 '17 at 12:55
12

I think you might need to add System.Data.DataSetExtensions Reference to your project before the AsEnumerable will work.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Stewbob
  • 16,759
  • 9
  • 63
  • 107
8

you need a reference to System.Data.DataSetExtensions

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Jonathan Bates
  • 1,835
  • 14
  • 22