DataTable does not contain definition for AsEnumerable

前端 未结 6 1790
轮回少年
轮回少年 2020-11-30 06:12

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

Project include

相关标签:
6条回答
  • 2020-11-30 06:48

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

    0 讨论(0)
  • 2020-11-30 06:48

    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?

    0 讨论(0)
  • 2020-11-30 06:48

    Try this code :

    DataSet1.mapDataTable.Select().AsEnumerable()

    0 讨论(0)
  • 2020-11-30 06:56

    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.

    0 讨论(0)
  • 2020-11-30 06:57

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

    Add this code:

    using System.Data.DataSetExtensions;
    
    0 讨论(0)
  • 2020-11-30 07:01

    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.

    0 讨论(0)
提交回复
热议问题