LinqToCSV如何消除空字符串周围的引号

时间:2015-01-09 03:34:45

标签: c# string linqtocsv

LinqToCSV似乎在空字符串周围添加引号 有没有设置关闭它? 在文档中找不到任何内容。 还试图发送空值而不是空字符串..相同的结果

这是使用LinqToCSV生成的CSV输出示例。 第5,6,7列是空字符串..它们最终成为最终CSV输出中的引用字符串

3223,2876,5591,9171068,"""""",10000442,A 1,"" ,012000016431,2.50,8,0,8,20150108224612 3223,2876,5591,9171068,"""""",10000442,A 2,"",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

这是参考的示例代码

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);

数据的所有属性都是纯字符串

例如,其中一列定义为

[CsvColumn(FieldIndex = 16, CanBeNull = true)]
public string TimeStamp { get; set; }

1 个答案:

答案 0 :(得分:0)

LinqToCsv v1.5开始,此行为无法再现。当我将TimeStamp字段的值设置为null时,不会引用该值。但是引用了String.Empty的值。

请考虑以下事项:

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; }
}

输出的文件内容为:

,,""

您可以使用this LinqPad query自行尝试。