none
Dataset.Select and foreach enumerator RRS feed

  • Question

  • Trying to convert an old application written in .net 2 to .net 4.  I several foreach enumerations on the Dataset.datatable that are using .Select to filter and/or sort the data.  The first issue I am getting is the foreach is getting the following error:

    foreach statement cannot operate on variables of type 'DataSchema.CodeDS.CODESDataTable' because 'DataSchema.CodeDS.CODESDataTable' does not contain a public definition for 'GetEnumerator'

    Also, doing a CodesDS.CODESDataTable.Select(), the Select() method does not exist any longer.  Have the enumerator and select capabilities be moved out of System.Data and into something else I need to include or reference?

    Appreciate any help.


    Smeese

    • Moved by CoolDadTx Tuesday, January 21, 2014 4:07 PM ADO.NET related
    Monday, January 20, 2014 11:50 PM

Answers

  • The standard ADO.NET types (DataSet, DataTable, etc) were created prior to generics and LINQ.  As such they don't implement the core IEnumerable<T> interface that is needed for LINQ to work.  To get them to work you'll need to add a method call to return IEnumerable<T>.  I personally use an extension method for this but you don't have to.  You should call OfType<T> where T is the type of the object you are enumerating.  Then you'll have access to all of LINQ

    var results = from d in DataSchema.CodeDS.CODESDataTable.Rows.OfType<CODESDataType>()
                        where ... select d;

    Michael Taylor
    http://msmvps.com/blogs/p3net

    Tuesday, January 21, 2014 4:06 PM

All replies

  • The standard ADO.NET types (DataSet, DataTable, etc) were created prior to generics and LINQ.  As such they don't implement the core IEnumerable<T> interface that is needed for LINQ to work.  To get them to work you'll need to add a method call to return IEnumerable<T>.  I personally use an extension method for this but you don't have to.  You should call OfType<T> where T is the type of the object you are enumerating.  Then you'll have access to all of LINQ

    var results = from d in DataSchema.CodeDS.CODESDataTable.Rows.OfType<CODESDataType>()
                        where ... select d;

    Michael Taylor
    http://msmvps.com/blogs/p3net

    Tuesday, January 21, 2014 4:06 PM
  • The code that worked with the old-style Typed DataSets should still work in .NET 4. It doesn't look like you're trying to use LINQ. Could you post some of your code?

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Thursday, January 23, 2014 3:46 PM