I have a model which I populate from a fairly complex query. The model looks like:
public class CSVExportViewModel
{
public long Id { get; set; }
public string StudentNo { get; set; }
public string StudentIdNumber { get; set; }
public string StudentName { get; set; }
public string Parent1IdNumber { get; set; }
public string Parent1Name { get; set; }
public string Parent2IdNumber { get; set; }
public string Parent2Name { get; set; }
/* etc... */
}
I now need to output the model to a CSV file using the code below:
List<CSVExportViewModel> data = GetData(dataset);
foreach (CSVExportViewModel item in data)
{
writer.WriteLine("'" + string.Join("','", item) + "'");
}
However, instead of converting the model into an array and outputting the individual items of the model, it just outputs "Models.CSVExportViewModel" to each line of the resulting CSV file.
How can I convert the model into a String array to get this working properly?
CSVExportViewModelis, but it definitely not string. So, C# compiler is printingclassname.CSVExportViewModelto string right? But I bet you haven't written aToStringmethod in that class, so it doesn't know how to output that to string. Thus you just get the class nameCSVExportViewModelitem.Textoritem.Valuefor this to work. Right now you are writing the name of the object containing various information to the file.