Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

linq

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();
like image 482
capdragon Avatar asked Jan 16 '23 12:01

capdragon


1 Answers

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.

like image 129
Tim Schmelter Avatar answered Feb 16 '23 01:02

Tim Schmelter