LINQ到数据集,DataTable.AsEnumerable()无法识别无法识别、数据、LINQ、AsEnumerable

2023-09-04 02:33:04 作者:宁缺毋滥

我是全新的,以 LINQ 和我想查询我的数据集它。于是我跟着这个例子来的那封信,这是行不通的。

我知道,我的数据表需要 .AsEnumerable 的结束,但它不被识别的 IDE 。我究竟做错了什么?我失去了未在本例中所示的参照/导入(不会是第一次在MSDN的例子是不完全正确),如果是的话,哪一个?或者是其它的什么东西?

样品code:

 导入系统
进口System.Linq的
进口System.Linq.Ex pressions
进口System.Collections.Generic
进口System.Data这
进口System.Data.SqlClient的
进口System.Data.Common
进口System.Globalization


//填充DataSet。
昏暗的DS作为新的数据集()
ds.Locale = CultureInfo.InvariantCulture
//看到装载数据的FillDataSet方法到数据集的话题。
FillDataSet(DS)

昏暗的产品作为数据表= ds.Tables(产品)

昏暗的查询=从产品中products.AsEnumerable()_
            选择产品
Console.WriteLine(产品名称:)
对于每个p在查询
    Console.WriteLine(p.Field(串)(姓名))
下一个
 

在我的项目中的引用:

 系统
System.Data这
System.Drawing中
System.Windows.Forms的
的System.Xml
 

解决方案

而类持有的扩展是在 System.Data这命名空间,它位于一个程序集不添加到您的项目在默认情况下。添加引用 System.Data.DataSetExtensions 到您的项目,它应该没问题。请记住,你已经添加了参考,也希望用在类中定义的扩展方法需要有一个使用语句System.Data这和任何类,即使。

LINQ GroupBy

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

解决方案

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.