简体   繁体   中英

DataTable does not contain a definition for 'AsEnumerable'

I have a DataTable, dt, that I am attempting to convert to an observable collection. I have included the reference to System.Data.DataSetExtensions. I am first converting the dt to to a DataView so that I can sort the data. I then convert back to a table. After I try to convert to IEnumerable. When I do this I get the error: Error CS1929 'DataTable' does not contain a definition for 'AsEnumerable'

Below is what I am trying. I am sure that I am missing something basic here. Again, I have double checkded the references. I have even removed and added the references back in again as my searches so far have indicated that this is the primary problem with this.

DataTable dt = GetData();
DataView dv = dt.DefaultView;
dv.Sort = "URLId desc"; //URLId is Column1
dt = dv.ToTable();
IEnumerable<MyClass> items = dt.AsEnumerable<MyClass>();  //Error occurs here
IList<MyClass> myItems = new ObservableCollection<MyClass>(items);

MyClass is a simple collection of string and date fields. Below is the constructor for the class.

public MyClass(string urlId, string urlAddress, string displayName)
{
    URLId = urlId;
    CommonName = displayName;
    URLAddress = urlAddress;
    DateLastTouched = DateTime.Now;
    Preview = urlAddress;
    Publish = false;                          
}

You're using

IEnumerable<MyClass> items = dt.AsEnumerable<MyClass>();

but i'm sure AsEnumerable is not AsEnumerable<T> , hence can you try

var items = dt.AsEnumerable().Cast<MyClass>()

EDIT (above returns EnumerableRowCollection<string> so you may even do

dt.AsEnumerable().Cast<MyClass>().AsEnumerable();

to get an IEnumerable<MyClass> )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM