问题描述
我是 LINQ
的新手,正在尝试用它查询我的 DataSet
.所以我跟着 this example 到了这封信,但它不起作用.
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.
我知道我的 DataTable
最后需要 .AsEnumerable
,但 IDE
无法识别.我究竟做错了什么?我是否遗漏了示例中未显示的引用/导入(不会是第一次 MSDN 示例不太正确),如果是,是哪一个?还是完全是别的东西?
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?
示例代码:
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
我的项目中的引用是:
System
System.Data
System.Drawing
System.Windows.Forms
System.Xml
推荐答案
虽然包含扩展的类位于 System.Data
命名空间中,但它位于未添加到您的程序集中的程序集中默认项目.将 System.Data.DataSetExtensions
的引用添加到您的项目中,应该没问题.请记住,即使在您添加了引用之后,任何希望使用该类中定义的扩展方法的类也需要具有 System.Data 的 using 语句.
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.
这篇关于LINQ to DataSet,DataTable.AsEnumerable() 无法识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!