Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataTable does not contain definition for AsEnumerable

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

like image 589
CJones Avatar asked Feb 09 '12 19:02

CJones


People also ask

What is DataTable AsEnumerable?

The enumerable object returned by the AsEnumerable method is permanently bound to the DataTable that produced it. Multiple calls to the AsEnumerable method will return multiple, independent queryable objects that are all bound to the source DataTable.

What is AsEnumerable?

AsEnumerable() in C# To cast a specific type to its IEnumerable equivalent, use the AsEnumerable() method. It is an extension method. The following is our array − int[] arr = new int[5]; arr[0] = 10; arr[1] = 20; arr[2] = 30; arr[3] = 40; arr[4] = 50; Now, get the IEnumerable equivalent.

Is DataTable IEnumerable?

A DataTable is not an IEnumerable by itself. Internally, the . AsEnumerable() call on it actually takes the . Rows collection and converts that into an IEnumerable(Of DataRow).

What is AsEnumerable in VB net?

AsEnumerable: casts a collection to IEnumerable of same type. This Lambda Expression sample casts array of strings to its corresponding IEnumerable. This Lambda Expression sample casts array of strings to its corresponding IEnumerable.


3 Answers

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

like image 105
Jon Skeet Avatar answered Oct 13 '22 20:10

Jon Skeet


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

Add this code:

using System.Data.DataSetExtensions;
like image 29
codemirror Avatar answered Oct 13 '22 22:10

codemirror


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?

like image 17
Panagiotis Kanavos Avatar answered Oct 13 '22 22:10

Panagiotis Kanavos