Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
627 views
in Technique[技术] by (71.8m points)

.net - 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...