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; }
+ /// Here, a LINQ query selects all products for "Netherlands" from the variable 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);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.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.
| Type: | + +bool |
+
| Default: | + +false | +
| Applies to: | + +Reading only | +
+ 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: +
+| id | +name | +last Name | +age | +CITY | +
|---|---|---|---|---|
| 1 | +John | +Doe | +15 | +Washington | +
| 2 | +Jane | +Doe | +20 | +New York | +
+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.
+