2012-09-19 2 views
2

this linq 문을 만들려고 할 때. 나는 다음과 같은 오류로 실행 :'System.Data.Common.DataRecordInternal'형식의 개체를 'System.Data.IDataReader'형식으로 캐스팅 할 수 없습니다.

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

이 내가 @SLaks promising answer 당 뭘하는지입니다.

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

당신이 당신의'TypeData' 클래스의 코드 구조를 게시 할 수 있습니까? – jp2code

+0

또한'TypeData obj = xxx.ToList()'가 설정되어있는 것처럼 보입니다. – jp2code

답변

6

reader.Cast<DbDataRecord> 시도하거나 대신 reader.Cast<IDataRecord> :

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.

+0

ToList 대신에'SingleOrDefault'가 필요합니까? – jp2code

+0

이것이 효과가 있습니다! 고마워 ... 2 분, 내가 너에게 신용을 줄 때까지. – capdragon

+0

@ jp2code : 네, ToList를 제거하고 대신 IEnumerable 을 사용했습니다. –

관련 문제