Reputation: 694
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;
}
Upvotes: 0
Views: 4395
Reputation: 3253
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>
)
Upvotes: 1