49

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.

starball
  • 20,030
  • 7
  • 43
  • 238
CJones
  • 587
  • 2
  • 5
  • 11

6 Answers6

83

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

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks for the quick response. Removed System.data.datasetextensions. Confirmed System.data.datasetextensions assembly. Form web.config: – CJones Feb 09 '12 at 20:08
  • 3
    @user1169290: Can you not just add it under references? I don't know whether having it under web.config makes a difference... – Jon Skeet Feb 09 '12 at 20:10
  • that's how I added it,I think that added the lines to the config file – CJones Feb 09 '12 at 20:16
  • Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. Compiler Error Message: CS0117: 'DataSet1.BIA_device_dp_mapDataTable' does not contain a definition for 'AsEnumerable' Source Error: Line 32: protected void Page_Load(object sender, EventArgs e) Line 33: { Line 34: var query1 = from mfg_nm in DataSet1.BIA_device_dp_mapDataTable.AsEnumerable() Line 35: Line 36: select mfg_nm; – CJones Feb 09 '12 at 20:31
  • @user1169290: That's very strange - can you reproduce it in a short but complete console program? If you try `DataTableExtensions.AsEnumerable(DataSet1.BIA_device_dp_mapDataTable)` what does it show? – Jon Skeet Feb 09 '12 at 20:34
  • I'm new to this, sorry if it's done incorrectly. I tried the following: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { DataTableExtensions.AsEnumerable(DataSet1.BIA_device_dp_mapDataTable); } } } And it returns "The name 'DataSet1' does not exist in the current context. Not sure what the issue is - I can bind to DataSet1 as an object data source – CJones Feb 09 '12 at 20:50
  • @user1169290: You'd have to actually have the data set available in your code. Did you try the other option I mentioned? – Jon Skeet Feb 09 '12 at 21:20
  • just see the console question. what was the other option? – CJones Feb 09 '12 at 21:24
  • @user1169290: Calling what's normally an extension method explicitly: `DataTableExtensions.AsEnumerable(DataSet1.BIA_device_dp_mapDataTable)` – Jon Skeet Feb 09 '12 at 21:26
  • I appreciate your persistence on this. Made this change to code and received the following errors. protected void Page_Load(object sender, EventArgs e) { var query = from mfg_nm in DataTableExtensions.AsEnumerable(DataSet1.BIA_device_dp_mapDataTable) select mfg_nm; } 'DataSet1.BIA_device_dp_mapDataTable' is a 'type', which is not valid in the given context The name 'DataSet1' does not exist in the current context – CJones Feb 09 '12 at 21:48
  • @user1169290: Okay, well that explains it. You're talking about a type, not an *instance* of the type. You need to actually *have* a DataTable to iterate over... – Jon Skeet Feb 09 '12 at 22:03
  • I guess that's my confusion - if i have an instance that's populated with data, how do I create a datable to query? – CJones Feb 10 '12 at 16:38
  • @user1169290: The DataTable *is* the instance I'm talking about. It sounds like you should take a few steps back from LINQ for the moment, and work out how to get some data from whatever your original data source is first. – Jon Skeet Feb 10 '12 at 17:14
  • the datatable has data. I can bind it to gridview or use objectdatasource and the they are populated with data. – CJones Feb 10 '12 at 17:49
  • @user1169290: But you don't *have* an instance, as far as we can see. `DataSet1.BIA_device_dp_mapDataTable` is a type (see the compiler error) so where's the instance? Maybe you've got some other variable, but you haven't shown it to us... – Jon Skeet Feb 10 '12 at 17:51
  • FWIW, in VS2015, have added a reference to System.Data.DataSetExtensions in a WinForms project. At my `using` directive, `using System.Data.DataSetExtensions`, I get an Intellisense error that `DataSetExtensions` does not exist in `System.Data`, and in my code, `myDataTable.asEnumerable` is underscored in red, with an error that DataTable does not support any such method. I open the "potential fixes" up at the `using` directive, which offers me the option to "remove unnecessary", and I simply close the popup. The using directive becomes grayed out, and the error lower in the code disappears. – Tim May 09 '17 at 12:48
  • But the project won't compile. Error CS0234 The type or namespace name 'DataSetExtensions' does not exist in the namespace 'System.Data' (are you missing an assembly reference?) – Tim May 09 '17 at 12:49
  • @Tim: Well yes, as my answer says, having a `using` directive (not a `using static`, which would now be valid) for `using System.Data.DataSetExtensions` is invalid. `DataSetExtensions` is a class, not a namespace. Remove that `using` directive. But it's not clear whether you really do have a reference to the `System.Data.DataSetExtensions` *assembly*. Do you see it in the "References" part of Solution Explorer? – Jon Skeet May 09 '17 at 13:19
  • Yes, the reference is valid, it is in Solution Explorer in the References section. I've removed the using directive. There's something screwy about my machine, I think. I've got the solution in a folder on Dropbox and it compiles at the office but not on my PC at home. I will reinstall the framework. – Tim May 09 '17 at 16:58
  • Adding a namespace System.Data in View worked for me – Atta H. Jul 11 '17 at 18:34
31

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

Add this code:

using System.Data.DataSetExtensions;
codemirror
  • 3,164
  • 29
  • 42
17

enter image description hereI 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.

MindRoasterMir
  • 324
  • 1
  • 2
  • 18
rjose
  • 557
  • 5
  • 13
17

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?

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
2

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.

Adam Cox
  • 3,341
  • 1
  • 36
  • 46
2

Try this code :

DataSet1.mapDataTable.Select().AsEnumerable()

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
Sandy
  • 21
  • 1