From 931bfedbf3bc4f078197370d5c94c73090e2c75f Mon Sep 17 00:00:00 2001 From: Himanshu Gupta Date: Fri, 16 Jun 2017 08:40:23 +0530 Subject: [PATCH] fixing less csv columns --- LINQtoCSV/FieldMapper.cs | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/LINQtoCSV/FieldMapper.cs b/LINQtoCSV/FieldMapper.cs index 9ef9dfc..5d1c8d7 100644 --- a/LINQtoCSV/FieldMapper.cs +++ b/LINQtoCSV/FieldMapper.cs @@ -50,7 +50,7 @@ public override string ToString() // IndexToInfo is used to quickly translate the index of a field // to its TypeFieldInfo. - protected TypeFieldInfo[] m_IndexToInfo = null; + protected List m_IndexToInfo = null; /// /// Contains a mapping between the CSV column indexes that will read and the property indexes in the business object. @@ -200,14 +200,14 @@ protected void AnalyzeType( // Initialize IndexToInfo int nbrTypeFields = m_NameToInfo.Keys.Count; - m_IndexToInfo = new TypeFieldInfo[nbrTypeFields]; + m_IndexToInfo = new List(nbrTypeFields); _mappingIndexes = new Dictionary(); int i=0; foreach (KeyValuePair kvp in m_NameToInfo) { - m_IndexToInfo[i++] = kvp.Value; + m_IndexToInfo.Insert(i++,kvp.Value); } // Sort by FieldIndex. Fields without FieldIndex will @@ -221,7 +221,7 @@ protected void AnalyzeType( // Note that for reading from a file with field names in the // first line, method ReadNames reworks IndexToInfo. - Array.Sort(m_IndexToInfo); + m_IndexToInfo.Sort(); // ---------- // Make sure there are no duplicate FieldIndices. @@ -286,7 +286,7 @@ public void WriteNames(List row) { row.Clear(); - for (int i = 0; i < m_IndexToInfo.Length; i++) + for (int i = 0; i < m_IndexToInfo.Count; i++) { TypeFieldInfo tfi = m_IndexToInfo[i]; @@ -310,7 +310,7 @@ public void WriteObject(T obj, List row) { row.Clear(); - for (int i = 0; i < m_IndexToInfo.Length; i++) + for (int i = 0; i < m_IndexToInfo.Count; i++) { TypeFieldInfo tfi = m_IndexToInfo[i]; @@ -433,9 +433,12 @@ public void ReadNames(IDataRow row) continue; } - m_IndexToInfo[_mappingIndexes[i]] = m_NameToInfo[row[i].Value]; + TypeFieldInfo tfi = m_NameToInfo[row[i].Value]; + m_IndexToInfo.Remove(tfi); + m_IndexToInfo.Insert(_mappingIndexes[i], tfi); - if (m_fileDescription.EnforceCsvColumnAttribute && (!m_IndexToInfo[i].hasColumnAttribute)) { + if (m_fileDescription.EnforceCsvColumnAttribute && !tfi.hasColumnAttribute) + { // enforcing column attr, but this field/prop has no column attr. throw new MissingCsvColumnAttributeException(typeof (T).ToString(), row[i].Value, m_fileName); } @@ -462,7 +465,7 @@ public List GetCharLengths() /// public T ReadObject(IDataRow row, AggregatedException ae) { //If there are more columns than the required - if (row.Count > m_IndexToInfo.Length) + if (row.Count > m_IndexToInfo.Count) { //Are we ignoring unknown columns? if (!m_fileDescription.IgnoreUnknownColumns) { @@ -476,7 +479,7 @@ public T ReadObject(IDataRow row, AggregatedException ae) { T obj = new T(); //If we will be using the mappings, we just iterate through all the cells in this row - int maxRowCount = _mappingIndexes.Count > 0 ? row.Count : Math.Min(row.Count, m_IndexToInfo.Length); + int maxRowCount = _mappingIndexes.Count > 0 ? row.Count : Math.Min(row.Count, m_IndexToInfo.Count); for (int i = 0; i < maxRowCount; i++) { TypeFieldInfo tfi; @@ -621,7 +624,7 @@ public T ReadObject(IDataRow row, AggregatedException ae) { // If only looking at fields with CsvColumn attribute, do ignore // fields that don't have that attribute. - for (int i = row.Count; i < m_IndexToInfo.Length; i++) + for (int i = row.Count; i < m_IndexToInfo.Count; i++) { TypeFieldInfo tfi = m_IndexToInfo[i];