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...
toList()) can be used to collect Stream elements into a list. We can leverage this to convert object to a list. //Similarly, you can change the method in the map to convert to the datatype you need.
CSV: Import the csv module in Python, create a csv writer object, and write the list of lists to the file in using the writerows() method on the writer object. Pandas: Import the pandas library, create a Pandas DataFrame, and write the DataFrame to a file using the DataFrame method DataFrame. to_csv('file. csv') .
Using Pandas to_csv() function To convert the list to csv, we need to convert from list to dataframe and then use the to_csv() function to convert dataframe to a csv file. In this example, we have first imported pandas library and then define the four lists and map it with its column using a dictionary.
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!
Because speed was mentioned in the question, my interest was piqued on just what the relative performances might be, and just how fast I could get it.
I know that StringBuilder was excluded, but it still felt like probably the fastest, and StreamWriter has of course the advantage of writing to either a MemoryStream or directly to a file, which makes it versatile.
So I knocked up a quick test.
I built a list half a million objects identical to yours.
Then I serialized with CsvSerializer, and with two hand-rolled tight versions, one using a StreamWriter to a MemoryStream and the other using a StringBuilder.
The hand rolled code was coded to cope with quotes but nothing more sophisticated. This code was pretty tight with the minimum I could manage of intermediate strings, no concatenation... but not production and certainly no points for style or flexibility.
But the output was identical in all three methods.
The timings were interesting:
Serializing half a million objects, five runs with each method, all times to the nearest whole mS:
StringBuilder 703 734 828 671 718 Avge= 730.8 MemoryStream 812 937 874 890 906 Avge= 883.8 CsvSerializer 1,734 1,469 1,719 1,593 1,578 Avge= 1,618.6
This was on a high end i7 with plenty of RAM.
Other things being equal, I would always use the library.
But if a 2:1 performance difference became critical, or if RAM or other issues turned out to exaggerate the difference on a larger dataset, or if the data were arriving in chunks and was to be sent straight to disk, I might just be tempted...
Just in case anyone's interested, the core of the code (for the StringBuilder version) was
private void writeProperty(StringBuilder sb, string value, bool first, bool last) { if (! value.Contains('\"')) { if (!first) sb.Append(','); sb.Append(value); if (last) sb.AppendLine(); } else { if (!first) sb.Append(",\""); else sb.Append('\"'); sb.Append(value.Replace("\"", "\"\"")); if (last) sb.AppendLine("\""); else sb.Append('\"'); } } private void writeItem(StringBuilder sb, Test item) { writeProperty(sb, item.Id.ToString(), true, false); writeProperty(sb, item.Name, false, false); writeProperty(sb, item.CreatedDate, false, false); writeProperty(sb, item.DueDate, false, false); writeProperty(sb, item.ReferenceNo, false, false); writeProperty(sb, item.Parent, false, true); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With