public CsvRecordIterator(CsvDataSource datasource, CsvReader reader) { _reader = reader; _builder = new RecordBuilder(datasource); // index here is random 0-n. index[0] gives the column no in the CSV // file, while colname[0] gives the corresponding column name. int columnSize = datasource.GetColumns().Count; _index = new int[columnSize]; _column = new Column[columnSize]; // skip the required number of lines before getting to the data for (int ix = 0; ix < datasource.SkipLines; ix++) { _reader.Next(); } // learn column indexes from header line (if there is one) String[] header; if (datasource.HasHeader) { header = _reader.Next(); } else { // find highest column number int high = datasource.GetColumns().Select(c => Int32.Parse(c.GetName())).Concat(new[] { 0 }).Max(); // build corresponding index header = new string[high]; for (int ix = 0; ix < high; ix++) { header[ix] = "" + (ix + 1); } } // build the 'index' and 'column' indexes int count = 0; foreach (var column in datasource.GetColumns()) { for (int ix = 0; ix < header.Length; ix++) { if (header[ix].Equals(column.GetName())) { _index[count] = ix; _column[count++] = column; break; } } } FindNextRecord(); }
public CsvRecordIterator(CsvDataSource datasource, CsvReader reader) { _reader = reader; _builder = new RecordBuilder(datasource); // index here is random 0-n. index[0] gives the column no in the CSV // file, while colname[0] gives the corresponding column name. int columnSize = datasource.GetColumns().Count; _index = new int[columnSize]; _column = new Column[columnSize]; // skip the required number of lines before getting to the data for (int ix = 0; ix < datasource.SkipLines; ix++) { _reader.Next(); } // learn column indexes from header line (if there is one) String[] header; if (datasource.HasHeader) { header = _reader.Next(); } else { // find highest column number int high = datasource.GetColumns().Select(c => Int32.Parse(c.GetName())).Concat(new[] {0}).Max(); // build corresponding index header = new string[high]; for (int ix = 0; ix < high; ix++) { header[ix] = "" + (ix + 1); } } // build the 'index' and 'column' indexes int count = 0; foreach (var column in datasource.GetColumns()) { for (int ix = 0; ix < header.Length; ix++) { if (header[ix].Equals(column.GetName())) { _index[count] = ix; _column[count++] = column; break; } } } FindNextRecord(); }