@@ -233,17 +233,12 @@ public static IEnumerable<object[]> AsEnumerable(this IDataReader reader)
233233 }
234234 }
235235
236- /// <summary>
237- /// Enumerates all the remaining values of the current result set of a data reader.
238- /// </summary>
239- /// <param name="reader">The reader to enumerate.</param>
240- /// <param name="ordinals">The limited set of ordinals to include. If none are specified, the returned objects will be empty.</param>
241- /// <returns>An enumeration of the values returned from a data reader.</returns>
242- public static IEnumerable < object [ ] > AsEnumerable ( this IDataReader reader , IEnumerable < int > ordinals )
236+
237+ static IEnumerable < object [ ] > AsEnumerableInternal ( this IDataReader reader , IEnumerable < int > ordinals , bool readStarted )
243238 {
244- if ( reader . Read ( ) )
239+ if ( readStarted || reader . Read ( ) )
245240 {
246- var o = ordinals . ToArray ( ) ;
241+ var o = ordinals as int [ ] ?? ordinals . ToArray ( ) ;
247242 var fieldCount = o . Length ;
248243 if ( fieldCount == 0 )
249244 {
@@ -266,6 +261,15 @@ public static IEnumerable<object[]> AsEnumerable(this IDataReader reader, IEnume
266261 }
267262 }
268263
264+ /// <summary>
265+ /// Enumerates all the remaining values of the current result set of a data reader.
266+ /// </summary>
267+ /// <param name="reader">The reader to enumerate.</param>
268+ /// <param name="ordinals">The limited set of ordinals to include. If none are specified, the returned objects will be empty.</param>
269+ /// <returns>An enumeration of the values returned from a data reader.</returns>
270+ public static IEnumerable < object [ ] > AsEnumerable ( this IDataReader reader , IEnumerable < int > ordinals )
271+ => AsEnumerableInternal ( reader , ordinals , false ) ;
272+
269273 /// <summary>
270274 /// Enumerates all the remaining values of the current result set of a data reader.
271275 /// </summary>
@@ -769,30 +773,18 @@ public static Dictionary<string, object> ToDictionary(this IDataRecord record, p
769773 public static Dictionary < string , object > ToDictionary ( this IDataRecord record , IEnumerable < string > columnNames )
770774 => ToDictionary ( record , new HashSet < string > ( columnNames ) ) ;
771775
772- static QueryResult < Queue < object [ ] > > RetrieveBlanksInternal ( IDataReader reader )
773- => new QueryResult < Queue < object [ ] > > (
774- Array . Empty < int > ( ) ,
775- Array . Empty < string > ( ) ,
776- new Queue < object [ ] > ( AsEnumerable ( reader , Enumerable . Empty < int > ( ) ) ) ) ;
777776
778- static QueryResult < Queue < object [ ] > > RetrieveInternal ( this IDataReader reader , int [ ] ordinals , string [ ] columnNames = null )
777+
778+ static QueryResult < Queue < object [ ] > > RetrieveInternal ( this IDataReader reader , int [ ] ordinals , string [ ] columnNames = null , bool readStarted = false )
779779 {
780780 var fieldCount = ordinals . Length ;
781781 if ( columnNames == null ) columnNames = ordinals . Select ( n => reader . GetName ( n ) ) . ToArray ( ) ;
782782 else if ( columnNames . Length != fieldCount ) throw new ArgumentException ( "Mismatched array lengths of ordinals and names." ) ;
783783
784- if ( fieldCount == 0 )
785- {
786- // No column names specified? Then return results, but empty ones. Simplify the results for counting.
787- return RetrieveBlanksInternal ( reader ) ;
788- }
789- else
790- {
791- return new QueryResult < Queue < object [ ] > > (
792- ordinals ,
793- columnNames ,
794- new Queue < object [ ] > ( AsEnumerable ( reader , ordinals ) ) ) ;
795- }
784+ return new QueryResult < Queue < object [ ] > > (
785+ ordinals ,
786+ columnNames ,
787+ new Queue < object [ ] > ( AsEnumerableInternal ( reader , ordinals , readStarted ) ) ) ;
796788 }
797789
798790 /// <summary>
@@ -908,13 +900,15 @@ public static QueryResult<Queue<object[]>> Retrieve(this IDbCommand command, str
908900 public static IEnumerable < T > Results < T > ( this IDataReader reader , IEnumerable < ( string Field , string Column ) > fieldMappingOverrides )
909901 where T : new ( )
910902 {
903+ if ( ! reader . Read ( ) ) return Enumerable . Empty < T > ( ) ;
904+
911905 var x = new Transformer < T > ( fieldMappingOverrides ) ;
912906 // Ignore missing columns.
913907 var columns = reader . GetMatchingOrdinals ( x . ColumnNames , true ) ;
914908
915909 return x . AsDequeueingEnumerable ( RetrieveInternal ( reader ,
916910 columns . Select ( c => c . Ordinal ) . ToArray ( ) ,
917- columns . Select ( c => c . Name ) . ToArray ( ) ) ) ;
911+ columns . Select ( c => c . Name ) . ToArray ( ) , readStarted : true ) ) ;
918912 }
919913
920914 /// <summary>
0 commit comments