Fastest way to convert a list of objects to csv with each object values in a new line

c#, csv

Solution

Use servicestack.text

Install-Package ServiceStack.Text

and then use the string extension methods `ToCsv(T)/FromCsv()`

Examples: https://github.com/ServiceStack/ServiceStack.Text

Update: `Servicestack.Text` is now free also in v4 which used to be commercial. No need to specify the version anymore! Happy serializing!

Problem

I have a class as follows : ``` public class Test { public int Id {get;set;} public string Name { get; set; } public string CreatedDate {get;set;} public string DueDate { get; set; } public string ReferenceNo { get; set; } public string Parent { get; set; } } ``` and I have a list of Test objects ``` List<Test>testobjs=new List(); ``` Now I would like to convert it into csv in following format: "1,John Grisham,9/5/2014,9/5/2014,1356,0\n2,Stephen King,9/3/2014,9/9/2014,1367,0\n3,The Rainmaker,4/9/2014,18/9/2014,1"; I searched for "Converting list to csv c#" and I got solutions as follows: ``` string.Join(",", list.Select(n => n.ToString()).ToArray()) ``` But this will not put the \n as needed i.e for each object Is there any fastest way other than string building to do this? Please help...

Original source

Related problems