From bdec5090f2b1dd9f76da17db1c43e562e5c93273 Mon Sep 17 00:00:00 2001 From: James Flaherty Date: Sun, 9 Oct 2016 10:30:47 -0400 Subject: [PATCH] Addressed issue with raw data read re-using the data row --- LINQtoCSV.Tests/CsvContextReadTests.cs | 50 ++++++++++++++++++++++++++ LINQtoCSV/CsvContext.cs | 8 ++++- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/LINQtoCSV.Tests/CsvContextReadTests.cs b/LINQtoCSV.Tests/CsvContextReadTests.cs index b8f57d8..34966f1 100644 --- a/LINQtoCSV.Tests/CsvContextReadTests.cs +++ b/LINQtoCSV.Tests/CsvContextReadTests.cs @@ -367,5 +367,55 @@ public void FileWithUnknownColumns_ShouldDiscardColumns() { AssertRead(input, description, expected); } + + + [TestMethod()] + public void GoodFileShouldReadRawData() + { + var description = new CsvFileDescription + { + SeparatorChar = ',', + FirstLineHasColumnNames = false + }; + + string input = +@"1,John,Doe,15,Washington +2,Jane,Doe,20,New York +"; + var expected = new[] + { + new TestDataRow + { + new DataRowItem("1", 1), + new DataRowItem("John", 1), + new DataRowItem("Doe", 1), + new DataRowItem("15", 1), + new DataRowItem("Washington", 1) + }, + new TestDataRow + { + new DataRowItem("2", 2), + new DataRowItem("Jane", 2), + new DataRowItem("Doe", 2), + new DataRowItem("20", 2), + new DataRowItem("New York", 2) + } + }; + + AssertRead(input, description, expected); + + } + + public class TestDataRow : List, IDataRow, IAssertable + { + public void AssertEqual(TestDataRow other) + { + for (var i = 0; i < this.Count; i++) + { + Assert.AreEqual(other[i].Value, this[i].Value); + Assert.AreEqual(other[i].LineNbr, this[i].LineNbr); + } + } + } } } diff --git a/LINQtoCSV/CsvContext.cs b/LINQtoCSV/CsvContext.cs index 62d799c..1e08aa8 100644 --- a/LINQtoCSV/CsvContext.cs +++ b/LINQtoCSV/CsvContext.cs @@ -178,7 +178,13 @@ private IEnumerable ReadData( { if (readingRawDataRows) { - obj = row as T; + var newRow = new T() as IDataRow; + for (var i = 0; i < row.Count; i++) + { + newRow.Add(row[i]); + } + + obj = newRow as T; } else {