Permalink
Browse files

When iterating the records multiple times a CsvReaderException will n…

…ow be thrown. This is to help stop confusion when 0 results are returned the second iteration.
  • Loading branch information...
1 parent 06148e2 commit efb2bf62590e54ae95540283fa62699f0096e199 @JoshClose committed Jan 4, 2013
Showing with 21 additions and 1 deletion.
  1. +6 −1 src/CsvHelper.Tests/CsvReaderTests.cs
  2. +15 −0 src/CsvHelper/CsvReader.cs
@@ -834,7 +834,12 @@ public void MultipleGetRecordsCalls()
csvReader.Configuration.IsStrictMode = false;
var records = csvReader.GetRecords<TestRecord>();
Assert.AreEqual( 2, records.Count() );
- Assert.AreEqual( 0, records.Count() );
+ try
+ {
+ records.Count();
+ Assert.Fail();
+ }
+ catch( CsvReaderException ) {}
}
}
View
@@ -34,6 +34,7 @@ public class CsvReader : ICsvReader
private string[] headerRecord;
private ICsvParser parser;
private int currentIndex = -1;
+ private bool doneReading;
private readonly Dictionary<string, List<int>> namedIndexes = new Dictionary<string, List<int>>();
#if !NET_2_0
private readonly Dictionary<Type, Delegate> recordFuncs = new Dictionary<Type, Delegate>();
@@ -151,6 +152,15 @@ public virtual bool Read()
{
CheckDisposed();
+ if( doneReading )
+ {
+ const string message = "The reader has already exhausted all records. " +
+ "If you would like iterate the records more than " +
+ "once, store the records in memory. i.e. Use " +
+ "CsvReader.GetRecords<T>().ToList()";
+ throw new CsvReaderException( message );
+ }
+
if( configuration.HasHeaderRecord && headerRecord == null )
{
headerRecord = parser.Read();
@@ -165,6 +175,11 @@ public virtual bool Read()
currentIndex = -1;
hasBeenRead = true;
+ if( currentRecord == null )
+ {
+ doneReading = true;
+ }
+
return currentRecord != null;
}

0 comments on commit efb2bf6

Please sign in to comment.