DataTable does not contain definition for AsEnumerable

喜欢而已 提交于 2019-11-26 16:31:33

问题


Using linq to query a datatable returns the following error: CS0117: 'DataSet1.map DataTable' does not contain a definition for 'AsEnumerable'

Project includes reference for 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

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

thanks in advance for your help


回答1:


The method you want is in the System.Data namespace, so that using directive is fine, but you also need a reference to the System.Data.DataSetExtensions assembly. Are you sure you've got that reference as an assembly reference?

It's not clear why you've got a using directive for a System.Data.DataSetExtensions namespace - does that not raise an error?

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.)




回答2:


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.

Also note that you only need to use the System.Data namespace.

BTW mapDataTable is a DataTable, right?




回答3:


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?)

Added

using System.Data;

Added "System.Data.DataSetExtensions" to the References section. It resolved the problem.




回答4:


Add System.Data.DataSetExtensions from "nuget" or "add reference"

Add this code:

using System.Data.DataSetExtensions;



回答5:


Google search "system.data.datatable does not contain a definition for asenumerable" brought me here, and my trouble was missing:

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.



来源:https://stackoverflow.com/questions/9217272/datatable-does-not-contain-definition-for-asenumerable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!