Home > OS >  How to get the last item in foreach loop writing to CSV?
How to get the last item in foreach loop writing to CSV?

Time:05-21

I'm having trouble getting the last item in the loop Writing to CSV, I don't want the last item in CSV to have a , at the end.

This is the public string to write my controller to CSV

    public string WriteTsv<T>(IEnumerable<T> data)
    {
        StringBuilder output = new StringBuilder();
        PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T));
        foreach (PropertyDescriptor prop in props)
        {
            output.Append(prop.DisplayName); // header
            output.Append(", \t");
        }
        output.AppendLine();
        foreach (T item in data) 
        {
            foreach (PropertyDescriptor prop in props)
            {
                output.Append(prop.Converter.ConvertToString(
                     prop.GetValue(item)));
                output.Append(", \t");
            }
            output.AppendLine();
        }
        return output.ToString();
    }

I've been stuck on this for a while now hoping anyone could help.

CodePudding user response:

Given your code, the simplest approach would be to remove the last 3 characters (final ", \t") before each call to AppendLine() using the fact that the StringBuilder.Length property is writeable.

So, instead of just

output.AppendLine();

do

output.Length -=3;
output.AppendLine();

From the same documentation it says,

If the specified length is less than the current length, the current StringBuilder object is truncated to the specified length.

You will, of course, need to do some error checking if you think there could be a string of less than length 3. You may also want to watch out for properties whose string value actually end in , \t...

CodePudding user response:

You can use String.Join to combine an IEnumerable<T> with a separator and then use AppendLine to add to the output. I used Cast<T>() to convert the IEnumerable implementation from the legacy PropertyDescriptorCollection to a modern IEnumerable<T>. (Unfortunately, a lot of C# hasn't been updated to properly support IEnumerable<T>.)

public string WriteTsv<T>(IEnumerable<T> data) {
    var output = new StringBuilder();
    var props = TypeDescriptor.GetProperties(typeof(T)).Cast<PropertyDescriptor>();
    // header
    output.AppendLine(String.Join(",", props.Select(prop => prop.DisplayName)));
    // data
    foreach (T item in data)
        output.AppendLine(String.Join(",", props.Select(prop => prop.Converter.ConvertToString(prop.GetValue(item)))));
    return output.ToString();
}
  • Related