LinqToCSV返回错误的列顺序

时间:2014-02-05 14:09:32

标签: c# export-to-csv

我正在使用LinqToCSV创建通过电子邮件发送给用户的CSV。该文件似乎生成正常,除了我打开文件;列不是我在Linq查询中指定的确切顺序:

System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.IO.TextWriter txt = new System.IO.StreamWriter(ms);

CsvFileDescription outputFileDescription = new CsvFileDescription
{
    SeparatorChar = ',',
    FirstLineHasColumnNames = true,
    FileCultureName = "en-US"
};


CsvContext csv = new CsvContext();

csv.Write(childOrders, txt, outputFileDescription);
txt.Flush();

ms.Position = 0;

1 个答案:

答案 0 :(得分:1)

转到您的对象“ childOrders”,然后在属性上方添加属性

[CsvColumn(Name = "ColumnName", FieldIndex = 1)]
public string ColumnName {get;set;}

[CsvColumn(Name = "ColumnName2", FieldIndex = 2)]
public string ColumnName2 {get;set;}