.net LINQ to DataSet,无法识别 DataTable.AsEnumerable()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3949302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:49:07  来源:igfitidea点击:

LINQ to DataSet, DataTable.AsEnumerable() not recognized

.netlinqvisual-studio-2010dataset

提问by GSTD

I am brand new to LINQand am trying to query my DataSetwith it. So I followed this exampleto the letter, and it does not work.

我是全新的,LINQ并试图DataSet用它来查询我的。所以我按照这个例子来信,但它不起作用。

I know that my DataTableneeds the .AsEnumerableon 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?

我知道我最终DataTable需要.AsEnumerable,但它没有被IDE. 我究竟做错了什么?我是否缺少示例中未显示的参考/导入(不是第一次 MSDN 示例不太正确),如果是,是哪个?或者它完全是别的东西?

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

回答by tvanfosson

While the class holding the extensions is in the System.Datanamespace, it's located in an assembly that isn't added to your project by default. Add a reference to System.Data.DataSetExtensionsto 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.

虽然包含扩展的类System.Data位于命名空间中,但它位于默认情况下未添加到项目的程序集中。添加System.Data.DataSetExtensions对您的项目的引用,应该没问题。请记住,即使在您添加了引用之后,任何希望使用类中定义的扩展方法的类也需要有一个用于 System.Data 的 using 语句。

回答by Stewbob

I think you might need to add System.Data.DataSetExtensionsReference to your project before the AsEnumerablewill work.

我认为您可能需要在System.Data.DataSetExtensions您的项目中添加Reference 才能AsEnumerable工作。

回答by Jonathan Bates

you need a reference to System.Data.DataSetExtensions

你需要参考 System.Data.DataSetExtensions