4

LinqToCSV seems to be adding quotes around empty strings Is there a setting to turn this off ? Could not find anything in the documentation. Also tried sending null values instead of empty strings.. same result

this is a sample CSV output generated using LinqToCSV. Columns 5,6,7 are empty strings.. they turn out to be quoted strings in final CSV output

3223,2876,5591,9171068,"","","",10000442,A1,"",012000016431,2.50,8,0,8,20150108224612 3223,2876,5591,9171068,"","","",10000442,A2,"",012000016431,2.50,8,0,8,20150108224612 3223,2876,5591,9171068,"","","",10000442,A3,"",012000043000,1.75,8,0,8,20150108224612 3223,2876,5591,9171068,"","","",10000442,A4,"",012000043000,1.75,8,0,8,20150108224612 3223,2876,5591,9171068,"","","",10000442,A5,"",012000043000,1.75,8,0,8,20150108224612 3223,2876,5591,9171068,"","","",10000442,A6,"",012000110467,1.75,8,0,8,20150108224612

this is the sample code for reference

CsvFileDescription fd = new CsvFileDescription();
fd.FirstLineHasColumnNames = false;
fd.QuoteAllFields = false;
fd.EnforceCsvColumnAttribute = true;            
fd.SeparatorChar = Globals.Separator[0];

CsvContext cc = new CsvContext();
cc.Write(data, txtFileName, fd);

All properties of data are plain strings

for example one of the columns is defined as

[CsvColumn(FieldIndex = 16, CanBeNull = true)]
public string TimeStamp { get; set; }
Maverick
  • 1,396
  • 5
  • 22
  • 42
SSS
  • 281
  • 2
  • 4
  • It would be good to add some sample data and actual code.. "Seems to be adding" seems to indicate, that you yourself aren't sure. In addition, please indicate if you tried something.. like searching documentation? – Vikas Gupta Jan 09 '15 at 03:45
  • Did you ever manage to find a solution to this issue as I need to solve it as well! Thanks – Sergio Dec 14 '15 at 09:18

1 Answers1

1

This behavior is not reproducible as of LinqToCsv v1.5. When I set the value of the TimeStamp field to null, the value is not quoted. However a value of String.Empty is quoted.

Consider the following:

void Main()
{
    var fd = new CsvFileDescription();
    fd.FirstLineHasColumnNames = false;
    fd.QuoteAllFields = false;
    fd.EnforceCsvColumnAttribute = true;
    fd.SeparatorChar = ',';

    var txtFileName = @"C:\temp\linqtocsv.csv";
    var data = new[] { new Data { TimeStamp = null, Foo = null, Bar = "" } };
    var cc = new CsvContext();
    cc.Write(data, txtFileName, fd);
}

class Data
{
    [CsvColumn(FieldIndex = 0, CanBeNull = true)]
    public string TimeStamp { get; set; }
    [CsvColumn(FieldIndex = 1, CanBeNull = true)]
    public string Foo { get; set; }
    [CsvColumn(FieldIndex = 2, CanBeNull = true)]
    public string Bar { get; set; }
}

The contents of the file which is output is:

,,""

You can try it yourself with this LinqPad query.

codekaizen
  • 26,990
  • 7
  • 84
  • 140