From ba1b807dcd9bdf26fa92fd5af6f5ae579488cec4 Mon Sep 17 00:00:00 2001 From: Manny Lertpatthanakul Date: Sat, 14 Jul 2018 09:32:40 -0400 Subject: [PATCH] Add ability to specify the equality comparer used for matching column names. Useful for allowing for case-insensitive comparisons --- LINQtoCSV/CsvFileDescription.cs | 8 +++++++- LINQtoCSV/FieldMapper.cs | 4 +++- TestConsoleApplication/ProductData.cs | 2 +- TestConsoleApplication/Program.cs | 3 ++- .../TestFiles/output_anon.csv | 6 +++--- .../TestFiles/output_names_nl.csv | Bin 882 -> 882 bytes .../TestFiles/output_newdata_names_nl.csv | Bin 652 -> 652 bytes 7 files changed, 16 insertions(+), 7 deletions(-) diff --git a/LINQtoCSV/CsvFileDescription.cs b/LINQtoCSV/CsvFileDescription.cs index b983ca8..f018586 100644 --- a/LINQtoCSV/CsvFileDescription.cs +++ b/LINQtoCSV/CsvFileDescription.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Globalization; using System.Text; @@ -90,7 +91,12 @@ public int MaximumNbrExceptions public bool UseFieldIndexForReadingData { get; set; } public bool UseOutputFormatForParsingCsvValue { get; set; } public bool IgnoreTrailingSeparatorChar { get; set; } - + + /// + /// Allows for override of the string comparison strategy + /// + public IEqualityComparer ColumnEqualityComparer { get; set; } + /// /// If set to true, wil read only the fields specified as attributes, and will discard other fields in the CSV file /// diff --git a/LINQtoCSV/FieldMapper.cs b/LINQtoCSV/FieldMapper.cs index 9ef9dfc..853fa4e 100644 --- a/LINQtoCSV/FieldMapper.cs +++ b/LINQtoCSV/FieldMapper.cs @@ -267,7 +267,9 @@ public FieldMapper(CsvFileDescription fileDescription, string fileName, bool wri m_fileDescription = fileDescription; m_fileName = fileName; - m_NameToInfo = new Dictionary(); + m_NameToInfo = fileDescription.ColumnEqualityComparer != null ? + new Dictionary(fileDescription.ColumnEqualityComparer) : + new Dictionary(); AnalyzeType( typeof(T), diff --git a/TestConsoleApplication/ProductData.cs b/TestConsoleApplication/ProductData.cs index 847c9f2..bbe0f1e 100644 --- a/TestConsoleApplication/ProductData.cs +++ b/TestConsoleApplication/ProductData.cs @@ -25,7 +25,7 @@ class ProductData public DateTime launchTime; // Can use both fields and properties - [CsvColumn(FieldIndex = 4, CanBeNull = false, OutputFormat = "#,000.000")] + [CsvColumn(FieldIndex = 4, CanBeNull = false, OutputFormat = "#,000.000", Name = "Weight")] public double weight { get; set; } // Following field has no CsvColumn attribute. diff --git a/TestConsoleApplication/Program.cs b/TestConsoleApplication/Program.cs index e82ff17..60fdaf9 100644 --- a/TestConsoleApplication/Program.cs +++ b/TestConsoleApplication/Program.cs @@ -29,7 +29,8 @@ static void Main(string[] args) SeparatorChar = ',', // default is ',' FirstLineHasColumnNames = true, EnforceCsvColumnAttribute = false, // default is false - FileCultureName = "en-US" // default is the current culture + FileCultureName = "en-US", // default is the current culture + ColumnEqualityComparer = StringComparer.CurrentCultureIgnoreCase }; try diff --git a/TestConsoleApplication/TestFiles/output_anon.csv b/TestConsoleApplication/TestFiles/output_anon.csv index f22ff9c..4f91285 100644 --- a/TestConsoleApplication/TestFiles/output_anon.csv +++ b/TestConsoleApplication/TestFiles/output_anon.csv @@ -1,4 +1,4 @@ Markup,InShops,ProductName -39150.0,2/01/1985 12:00:00 AM,mouse trap -270.060,23/05/2008 12:00:00 AM,moonbuggy -6004.0,29/02/2004 12:00:00 AM,dog house +39150.0,1/2/1985 12:00:00 AM,mouse trap +270.060,5/23/2008 12:00:00 AM,moonbuggy +6004.0,2/29/2004 12:00:00 AM,dog house diff --git a/TestConsoleApplication/TestFiles/output_names_nl.csv b/TestConsoleApplication/TestFiles/output_names_nl.csv index f7ec0a3f0f5ca6e050bffdf9eab2e6d3eafab2fd..4a701a7f10132e0e987187c2467e3451df96958e 100644 GIT binary patch delta 12 Tcmeyw_K9tR8DscHb8BV*Ams!# delta 12 Tcmeyw_K9tR8DsfIb8BV*A%z50 diff --git a/TestConsoleApplication/TestFiles/output_newdata_names_nl.csv b/TestConsoleApplication/TestFiles/output_newdata_names_nl.csv index 35c7ee071d269c242e2f3f49cfcaf62619a0e2fd..d8ba3adda4b62a996db0fc375a9701bea35b867f 100644 GIT binary patch delta 37 scmeBS?O~l@#uz@)d?Tw7gD!*N#<%i}+=dK>3