C# DataTable 不包含 AsEnumerable 的定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9217272/
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
DataTable does not contain definition for AsEnumerable
提问by user1169290
Using linq to query a datatable returns the following error: CS0117: 'DataSet1.map DataTable' does not contain a definition for 'AsEnumerable'
使用 linq 查询数据表返回以下错误: CS0117: 'DataSet1.map DataTable' 不包含 'AsEnumerable' 的定义
Project includes reference for System.Data.Datasetextensions.
项目包括对 System.Data.Datasetextensions 的参考。
Here's the code.
这是代码。
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Reflection;
using System.Data;
using System.Linq;
using System.Data.Linq;
using System.Data.Common;
using System.Data.DataSetExtensions;
using System.Linq.Expressions;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
protected void Page_Load(object sender, EventArgs e)
{
var query1 = from mfg_nm in DataSet1.mapDataTable.AsEnumerable()
select mfg_nm;
}
running it w/out AsEnumerable() results in
在没有 AsEnumerable() 的情况下运行它会导致
var query1 = from mfg_nm in DataSet1.mapDataTable
select mfg_nm;
CS1660: Cannot convert lambda expression to type 'string' because it is not a delegate type
CS1660:无法将 lambda 表达式转换为类型“字符串”,因为它不是委托类型
thanks in advance for your help
在此先感谢您的帮助
采纳答案by codemirror
Add System.Data.DataSetExtensionsfrom "nuget" or "add reference"
从“ nuget”或“添加引用”添加System.Data.DataSetExtensions
Add this code:
添加此代码:
using System.Data.DataSetExtensions;
回答by Jon Skeet
The method you wantis in the System.Datanamespace, so that usingdirective is fine, but you also need a reference to the System.Data.DataSetExtensionsassembly. Are you sureyou've got that reference as an assemblyreference?
您想要的方法在System.Data命名空间中,因此该using指令没问题,但您还需要对System.Data.DataSetExtensions程序集的引用。您确定将该引用作为程序集引用吗?
It's not clear why you've got a using directive for a System.Data.DataSetExtensionsnamespace- does that not raise an error?
不清楚为什么你有一个用于System.Data.DataSetExtensions命名空间的 using 指令- 这不会引发错误吗?
What is the exact error with the AsEnumerable()call? (I'm surprised about the error you're getting with the second form... that's not the error I'd have expected.)
AsEnumerable()调用的确切错误是什么?(我对您在第二种形式中遇到的错误感到惊讶……这不是我所期望的错误。)
回答by Panagiotis Kanavos
In all cases where this happens, the reference to System.Data.DataSetExtensions.dll was missing. If in doubt, try creating a simple console project targeting .NET 4 with a reference to System.Data.DataSetExtensions.dll, to verify that adding the reference actually works.
在发生这种情况的所有情况下,都缺少对 System.Data.DataSetExtensions.dll 的引用。如果有疑问,请尝试创建一个针对 .NET 4 的简单控制台项目,并引用 System.Data.DataSetExtensions.dll,以验证添加引用是否有效。
Also note that you only need to use the System.Data namespace.
另请注意,您只需要使用 System.Data 命名空间。
BTW mapDataTable is a DataTable, right?
顺便说一句 mapDataTable 是一个数据表,对吧?
回答by Adam Cox
Google search "system.data.datatable does not contain a definition for asenumerable" brought me here, and my trouble was missing:
谷歌搜索“system.data.datatable不包含asenumerable的定义”把我带到这里,我的麻烦不见了:
using System.Data;
Due to my implement the error message was a bit misleading. Hence, my answer to this question. Code was like...
由于我的实现,错误消息有点误导。因此,我对这个问题的回答。代码就像...
public List<mytype> MyMethod(params) {
return new mynamespace.myclasslib.myclass().GetDataTable(params).AsEnumerable()
.etc
}
Once I tried to explicitly declare the DataTable, it became obvious that I was missing the using statement.
一旦我尝试显式声明 DataTable,很明显我缺少 using 语句。
回答by rjose
I got this error message:
'System.Data.DataTable' does not contain a definition for 'AsEnumerable' and no extension method 'AsEnumerable' accepting a first argument of type 'System.Data.DataTable' could be found (are you missing a using directive or an assembly reference?)
我收到此错误消息:“System.Data.DataTable”不包含“AsEnumerable”的定义,并且找不到接受“System.Data.DataTable”类型的第一个参数的扩展方法“AsEnumerable”(您是否缺少使用指令或程序集引用?)
Added
添加
using System.Data;
Added "System.Data.DataSetExtensions" to the References section. It resolved the problem.
在参考部分添加了“System.Data.DataSetExtensions”。它解决了问题。

