-1

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?

9
  • I don't know what CSVExportViewModel is, but it definitely not string. So, C# compiler is printing classname. Commented Jan 9, 2017 at 11:32
  • You're telling it to write the CSVExportViewModel to string right? But I bet you haven't written a ToString method in that class, so it doesn't know how to output that to string. Thus you just get the class name Commented Jan 9, 2017 at 11:32
  • You need to output each property of CSVExportViewModel Commented Jan 9, 2017 at 11:32
  • You propably need to call item.Text or item.Value for this to work. Right now you are writing the name of the object containing various information to the file. Commented Jan 9, 2017 at 11:32
  • look for the definition of ToString() method on CSVExportViewModel Commented Jan 9, 2017 at 11:35

3 Answers 3

0

implement ToString() in CSVExportViewModel class:

public override string ToString()
{
    return Id + ", " + StudentNo + ", " + StudentIdNumber + ", " + StudentName + ", " +
    Parent1IdNumber + ", " + Parent1Name + ", " + Parent2IdNumber + ", " + Parent2Name;
}
Sign up to request clarification or add additional context in comments.

Comments

0

you can create an array on fly

foreach (CSVExportViewModel item in data)
{
    writer.WriteLine(string.Join(",",  new object[] {item.prop1, item.prop2, item.prop3 ...}));
}

Comments

0

Thanks all. @MathewJibin pointed me in the right direction. I implemented ToArray() in my model as follows:

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... */
    public String[] ToArray()
    {
        List<string> arr = new List<string>();

        foreach (var prop in typeof(CSVExportViewModel).GetProperties())
        {
            string value = "";
            if (prop.GetValue(this, null) != null)
            {
                value = prop.GetValue(this, null).ToString();
            }

            arr.Add(value);
        }

        return arr.ToArray();
    }
}

Then just had to modify the code to call item.ToArray();

Thanks all for the help!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.