Skip to main content
edited tags; edited title
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

Is this implementation of Converting a list to a CSV string csv ok

Tweeted twitter.com/#!/StackCodeReview/status/161727222074454016
added 488 characters in body
Source Link
dreza
  • 6.4k
  • 2
  • 29
  • 42

I want to be able to convert a list of objects into a string csv format. I've written this extension method below but have a feeling I'm missing something as this seems like potentially a common thing to want to do.

private static readonly char[] csvChars = new[] { ',', '"', ' ', '\n', '\r' };

public static string ToCSV<T>ToCsv<T>(this IEnumerable<T> source, Func<T, object> getItem)
{
    if ((source == null) || (getItem == null))
    {
        StringBuilderreturn string.Empty;
    }

    var builder = new StringBuilder();
    var items = from item in source.Select(getItem) 
                where item != null 
                select item.ToString();
    
    foreach (var objstr in sourceitems)
    {
        if (str.IndexOfAny(csvChars) > 0)
        {
            builder.Append(getItem(obj"\"").ToStringAppend(str).Append("\"").Append(", ");
 
        string}
 csv = builder.ToString();

     else
   if (csv.Length > 0)
  {
          return csv builder.TrimEndAppend(str).Append(", ".ToCharArray());
        else}
    }

    var csv = builder.ToString();

    return csv;
csv.Length > 0 ? csv.TrimEnd(", ".ToCharArray()) : csv;
}

Is there anything I can do to improve this or refactor to a more elegant or working solution. Or even an existing method out there already that I may have missed.

UPDATE: Updated to take into account quotations as per Jesse comments below.

I want to be able to convert a list of objects into a string csv format. I've written this extension method below but have a feeling I'm missing something as this seems like potentially a common thing to want to do.

    public static string ToCSV<T>(this IEnumerable<T> source, Func<T, object> getItem)
    {
        StringBuilder builder = new StringBuilder();

        foreach (var obj in source)
            builder.Append(getItem(obj).ToString()).Append(", ");
 
        string csv = builder.ToString();

        if (csv.Length > 0)
            return csv.TrimEnd(", ".ToCharArray());
        else
            return csv;
    }

Is there anything I can do to improve this or refactor to a more elegant or working solution. Or even an existing method out there already that I may have missed.

I want to be able to convert a list of objects into a string csv format. I've written this extension method below but have a feeling I'm missing something as this seems like potentially a common thing to want to do.

private static readonly char[] csvChars = new[] { ',', '"', ' ', '\n', '\r' };

public static string ToCsv<T>(this IEnumerable<T> source, Func<T, object> getItem)
{
    if ((source == null) || (getItem == null))
    {
        return string.Empty;
    }

    var builder = new StringBuilder();
    var items = from item in source.Select(getItem) 
                where item != null 
                select item.ToString();
    
    foreach (var str in items)
    {
        if (str.IndexOfAny(csvChars) > 0)
        {
            builder.Append("\"").Append(str).Append("\"").Append(", ");
        }
        else
        {
            builder.Append(str).Append(", ");
        }
    }

    var csv = builder.ToString();

    return csv.Length > 0 ? csv.TrimEnd(", ".ToCharArray()) : csv;
}

Is there anything I can do to improve this or refactor to a more elegant or working solution. Or even an existing method out there already that I may have missed.

UPDATE: Updated to take into account quotations as per Jesse comments below.

Added extra comments to ask for help
Source Link
dreza
  • 6.4k
  • 2
  • 29
  • 42

I want to be able to convert a list of objects into a string csv format. I've written this extension method below but have a feeling I'm missing something as this seems like potentially a common thing to want to do.

    public static string ToCSV<T>(this IEnumerable<T> source, Func<T, object> getItem)
    {
        StringBuilder builder = new StringBuilder();

        foreach (var obj in source)
            builder.Append(getItem(obj).ToString()).Append(", ");

        string csv = builder.ToString();

        if (csv.Length > 0)
            return csv.TrimEnd(", ".ToCharArray());
        else
            return csv;
    }

Is there anything I can do to improve this or refactor to a more elegant or working solution. Or even an existing method out there already that I may have missed.

I want to be able to convert a list of objects into a string csv format. I've written this extension method below but have a feeling I'm missing something as this seems like potentially a common thing to want to do.

    public static string ToCSV<T>(this IEnumerable<T> source, Func<T, object> getItem)
    {
        StringBuilder builder = new StringBuilder();

        foreach (var obj in source)
            builder.Append(getItem(obj).ToString()).Append(", ");

        string csv = builder.ToString();

        if (csv.Length > 0)
            return csv.TrimEnd(",".ToCharArray());
        else
            return csv;
    }

I want to be able to convert a list of objects into a string csv format. I've written this extension method below but have a feeling I'm missing something as this seems like potentially a common thing to want to do.

    public static string ToCSV<T>(this IEnumerable<T> source, Func<T, object> getItem)
    {
        StringBuilder builder = new StringBuilder();

        foreach (var obj in source)
            builder.Append(getItem(obj).ToString()).Append(", ");

        string csv = builder.ToString();

        if (csv.Length > 0)
            return csv.TrimEnd(", ".ToCharArray());
        else
            return csv;
    }

Is there anything I can do to improve this or refactor to a more elegant or working solution. Or even an existing method out there already that I may have missed.

Source Link
dreza
  • 6.4k
  • 2
  • 29
  • 42
Loading