0

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;                          
}
Daniel Lee
  • 674
  • 1
  • 9
  • 25
  • 2
    Possible duplicate of [DataTable does not contain definition for AsEnumerable](http://stackoverflow.com/questions/9217272/datatable-does-not-contain-definition-for-asenumerable) – Daniel Kelley Oct 01 '15 at 10:46
  • Have you added `System.Data.DataSetExtensions` ? – rbm Oct 01 '15 at 10:46
  • I thought I was clear in my post BUT yes, I have added System.Data.DataSetExtensions. The post referenced by Daniel Kelley indicates that the problem was a missing reference to System.Data,DataSetExtensions. That is why I am posting here. :) – Daniel Lee Oct 01 '15 at 10:48
  • I am not sure of why I am getting down votes. While I am getting the same error, I have triple checked that the reference to System.Data.DataSetExtensions has been added. – Daniel Lee Oct 01 '15 at 10:54
  • I've answered - see below – rbm Oct 01 '15 at 11:43

1 Answers1

1

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

rbm
  • 3,243
  • 2
  • 17
  • 28