diff --git a/LINQtoCSV.Tests/CsvContextReadTests.cs b/LINQtoCSV.Tests/CsvContextReadTests.cs index b8f57d8..ea14f8e 100644 --- a/LINQtoCSV.Tests/CsvContextReadTests.cs +++ b/LINQtoCSV.Tests/CsvContextReadTests.cs @@ -18,7 +18,7 @@ public void GoodFileUsingOutputFormatForParsingDatesCharUSEnglish() { SeparatorChar = ';', FirstLineHasColumnNames = false, - UseOutputFormatForParsingCsvValue = true, + UseOutputFormatForParsingCsvValue = true, EnforceCsvColumnAttribute = true, // default is false FileCultureName = "en-US" // default is the current culture }; @@ -216,7 +216,7 @@ two newlines and a quoted """"string"""""" dog house, ""45,230,990"",29 Feb 2004, , -56, True,"""", FF10, ""12,008"""; - var expected = new [] { + var expected = new[] { new ProductData { name = "moonbuggy", weight = 34.184, startDate = new DateTime(2008, 5, 23), launchTime = new DateTime(2009, 5, 5, 16, 11, 0), nbrAvailable = 1205, onsale = true, shopsAvailable = "Paris, New York", hexProductCode = 31, retailPrice = 540.12M, @@ -333,16 +333,17 @@ and a quoted ""string""" } [TestMethod()] - public void FileWithUnknownColumns_ShouldDiscardColumns() { + public void FileWithUnknownColumns_ShouldDiscardColumns() + { var description = new CsvFileDescription { SeparatorChar = ',', FirstLineHasColumnNames = true, IgnoreUnknownColumns = true, }; - + //The following input has 5 columns: Id | Name | Last Name | Age | City. Only the Name, Last Name and Age will be read. - + string input = @"Id,Name,Last Name,Age,City 1,John,Doe,15,Washington @@ -367,5 +368,57 @@ public void FileWithUnknownColumns_ShouldDiscardColumns() { AssertRead(input, description, expected); } + + [TestMethod()] + public void GoodFileWithIgnoringCaseOnColumnNames() + { // Arrange + + CsvFileDescription fileDescription_namesUs = new CsvFileDescription + { + SeparatorChar = ',', // default is ',' + FirstLineHasColumnNames = true, + EnforceCsvColumnAttribute = false, // default is false + FileCultureName = "en-US", // default is the current culture + IgnoreCaseOnColumnNames = true, + IgnoreTrailingSeparatorChar = true + + }; + + string testInput = +@"nAME, Weight, startDate, LAunchTime, nbrAVAILABLE,onsalE,shopsAVAilable, CODE, Price, Description, +moonbuggy, 34.184, 5/23/08, 5-May-2009 4:11 pm, 1205, true, ""Paris, New York"", 1F, $540.12, newly launched product, +""mouse trap"",45E-5, 1/2/1985, ""7 August 1988, 0:00 am"", ""4,030"", FALSE, ""This field has +a newline"", 100, ""$78,300"", ""This field has quotes(""""), and +two newlines +and a quoted """"string"""""" +dog house, ""45,230,990"",29 Feb 2004, , -56, True,"""", FF10, ""12,008"","; + + var expected = new[] { + new ProductData { + name = "moonbuggy", weight = 34.184, startDate = new DateTime(2008, 5, 23), launchTime = new DateTime(2009, 5, 5, 16, 11, 0), + nbrAvailable = 1205, onsale = true, shopsAvailable = "Paris, New York", hexProductCode = 31, retailPrice = 540.12M, + description = "newly launched product" + }, + new ProductData { + name = "mouse trap", weight = 45E-5, startDate = new DateTime(1985, 1, 2), launchTime = new DateTime(1988, 8, 7, 0, 0, 0), + nbrAvailable = 4030, onsale = false, shopsAvailable = @"This field has +a newline", hexProductCode = 256, retailPrice = 78300M, + description = @"This field has quotes(""), and +two newlines +and a quoted ""string""" + }, + new ProductData { + name = "dog house", weight = 45230990, startDate = new DateTime(2004, 2, 29), launchTime = default(DateTime), + nbrAvailable = -56, onsale = true, shopsAvailable = "", hexProductCode = 65296, retailPrice = 12008M, + description = null + } + }; + + // Act and Assert + + AssertRead(testInput, fileDescription_namesUs, expected); + + + } } } diff --git a/LINQtoCSV/CsvFileDescription.cs b/LINQtoCSV/CsvFileDescription.cs index b983ca8..ee9d726 100644 --- a/LINQtoCSV/CsvFileDescription.cs +++ b/LINQtoCSV/CsvFileDescription.cs @@ -96,6 +96,11 @@ public int MaximumNbrExceptions /// public bool IgnoreUnknownColumns { get; set; } + /// + /// If set to true, will read the column names while ignoring the case + /// + public bool IgnoreCaseOnColumnNames { get; set; } + // --------------- public CsvFileDescription() @@ -112,6 +117,7 @@ public CsvFileDescription() UseFieldIndexForReadingData = false; UseOutputFormatForParsingCsvValue = false; IgnoreUnknownColumns = false; + IgnoreCaseOnColumnNames = false; } } } diff --git a/LINQtoCSV/FieldMapper.cs b/LINQtoCSV/FieldMapper.cs index 9ef9dfc..8a81f56 100644 --- a/LINQtoCSV/FieldMapper.cs +++ b/LINQtoCSV/FieldMapper.cs @@ -267,7 +267,8 @@ public FieldMapper(CsvFileDescription fileDescription, string fileName, bool wri m_fileDescription = fileDescription; m_fileName = fileName; - m_NameToInfo = new Dictionary(); + //if case insensitive set then declare dictionary with ignorecase + m_NameToInfo = m_fileDescription.IgnoreCaseOnColumnNames ? new Dictionary(StringComparer.InvariantCultureIgnoreCase) : new Dictionary(); AnalyzeType( typeof(T), @@ -410,6 +411,7 @@ public void ReadNames(IDataRow row) int currentNameIndex = 0; for (int i = 0; i < row.Count; i++) { + if (!m_NameToInfo.ContainsKey(row[i].Value)) { //If we have to ignore this column if (m_fileDescription.IgnoreUnknownColumns) { diff --git a/article.htm b/article.htm index 55adc4b..dc29d2b 100644 --- a/article.htm +++ b/article.htm @@ -110,7 +110,7 @@

Reading from a file

Create a CsvFileDescription object, and initialize it with details about the file that you're going to read. It will look like this:
CsvFileDescription inputFileDescription = new CsvFileDescription
 {
-    SeparatorChar = ',', 
+    SeparatorChar = ',',
     FirstLineHasColumnNames = true
 };
@@ -147,7 +147,7 @@

Reading from a file

CsvFileDescription inputFileDescription = new CsvFileDescription
 {
-    SeparatorChar = ',', 
+    SeparatorChar = ',',
     FirstLineHasColumnNames = true
 };
 CsvContext cc = new CsvContext();
@@ -268,7 +268,7 @@ 

Writing an IEnumerable of anonymous type

// Write contents of productsNetherlands to file cc.Write( productsNetherlands, - "products-Netherlands.csv", + "products-Netherlands.csv", outputFileDescription);

Here, a LINQ query selects all products for "Netherlands" from the variable products, and returns an IEnumerable holding objects of some anonymous type that has the fields Name, LaunchDate, Price, and Description. The Write method then writes those objects to the file products-Netherlands.csv.

@@ -452,6 +452,8 @@

CsvFileDescription

  • IgnoreTrailingSeparatorChar
  • IgnoreUnknownColumns
  • + +
  • IgnoreCaseOnColumnNames
  • SeparatorChar

    @@ -879,7 +881,7 @@

    Example:


    Suppose you have the following class:
    -class Person 
    +class Person
     {
         [CsvColumn(Name = "Name")]
         public string Name { get ; set; }
    @@ -898,6 +900,80 @@ 

    Example:

    then the columns "Id" and "City" will be ignored without an exception.

    +

    IgnoreCaseOnColumnNames

    + + + + + + + + + + + + + + + + + + + + + +
    Type:bool
    Default:false
    Applies to:Reading only
    + +

    Example:

    +

    + If you dont want your class property name cases to match with the column names in the csv and do not want to use the additional name attribute just to match column names, you can set this attribute to true which will ignore casing in column name: +

    + + + + + + + + + + + + + + + + + + + + + + + + +
    idnamelast NameageCITY
    1JohnDoe15Washington
    2JaneDoe20New York
    +
    +Suppose you have the following class: +
    +class Person
    +{
    +    [CsvColumn]
    +    public string Name { get ; set; }
    +    [CsvColumn]
    +    public string LastName { get; set; }
    +    [CsvColumn]
    +    public int Age { get; set; }
    +}
    +
    +

    + Note that the input file has different case for column names than the class members. This discrepancy would normally cause an exception. +

    +

    + However, if you set

    fd.IgnoreCaseOnColumnNames = true;
    + + then the columns will match without an exception. +

    CsvColumn Attribute

    @@ -1820,6 +1896,15 @@

    History

    Courtesy of Oscar Mederos. + + 1.6 + 29 April 2016 + + Introduced option to ignore case on column names in the input file. + Courtesy of Shahzad Ahmad. + + + @@ -1829,9 +1914,3 @@

    New Features and Bug Fixes

    Details: https://github.com/mperdeck/LINQtoCSV

    - - - - - -