I have a list that I want to write to a CSV string.
The examples I found seem to be for separate lists of elements, I have several elements.
The code I have is
private static string CreateCSVTextFile<T>(List<T> data, string seperator = ",") where T : ExcelReport, new() { var objectType = typeof(T); var properties = objectType.GetProperties(); var currentRow = 0; var returnString = ""; foreach (var row in data) { var currentColumn = 0; var lineString = ""; foreach (var info in properties) { lineString = lineString + info.GetValue(row, null) + seperator; currentColumn++; } if (seperator != "") { lineString = lineString.Substring(0, lineString.Count() - 2); } returnString = returnString + Environment.NewLine + lineString; currentRow++; } return returnString; }
But when the list is large, this method takes a lot of time.
The class my list is based on the form:
internal class ClientMasterFile { public String COL1{ get; set; } public String COL2{ get; set; } public String COL3{ get; set; } public String COL4{ get; set; } public String COL5{ get; set; } public String COL6{ get; set; } public String COL7{ get; set; } public String COL8{ get; set; } public String COL9{ get; set; } public String COL10{ get; set; } public String COL11{ get; set; } public String COL12{ get; set; } }
Is there a faster way to do this using an extended version of String.Join?
thanks
source share