4

When trying to create this linq statement. I ran into the following error:

Unable to cast object of type 'System.Data.Common.DataRecordInternal' to type 'System.Data.IDataReader'

This is what I'm doing per @SLaks promising answer.

List<TypeData> = reader.Cast<IDataReader>()
   .Select(dr => new TypeData { Type = (string)dr["type"] })                
   .ToList();
4

1 回答 1

9

Try reader.Cast<DbDataRecord> or reader.Cast<IDataRecord> instead:

IEnumerable<TypeData> typeData = reader.Cast<IDataRecord>()
   .Select(dr => new TypeData { Type = (string)dr["type"] });

IDataRecord Interface

Provides access to the column values within each row for a DataReader, and is implemented by .NET Framework data providers that access relational databases.

于 2012-09-19T18:39:54.437 回答