Export to csv - Linq query

前端 未结 3 1913
感情败类
感情败类 2021-01-19 00:44

I have a class in linq that query db table like this, and the question is: How do I export that data to csv? I have tried link suggested and I am using linq2csv and still w

3条回答
  •  执笔经年
    2021-01-19 01:18

    LINQtoCSV doesn't preserve your column order unless you use the [CsvColumn(FieldIndex = 1)] attribute on all your properties. But who wants to do that? I would use ServiceStack.Text library. It preserves column orders by default. Works like this:

    string csv = ServiceStack.Text.CsvSerializer.SerializeToCsv<>(exportData);

提交回复
热议问题