0

I'm trying to convert the list to CSV string.list have 200+ properties.So I need properties as well values Example
Id,name,class,lastname....so on..
1,test,second,test....so on..
2,test2,second,test2...so on...

I used append with foreach

  foreach (var value in testData)
        {
            sb.Append(separator).Append(value);
            separator = ",";
              sb.Append(separator).Append(value);
            separator = ",";
        }

I need to pass 200 properties it increases the line of code.

4
  • 200 properties in a single object...? Two hundred? Wat? Somehow i get the feeling you did not explain your situation correctly... Commented Jun 6, 2017 at 18:40
  • Problably he wants an snippet to read the properties and values whitout need to write the properties name increasing his code. This is my guess... So, we need the source object and type and structure. Commented Jun 6, 2017 at 18:43
  • @AndrewPaes, i was almost suggesting in a comment to use reflection. But the 200+ properties thing just seems a little too weird in my eyes to be taken at face value. As you said, there are probably other types/structures/datasets involved, not simply properties... Commented Jun 6, 2017 at 18:45
  • @elgonzo yes in one class have 200+ properties which I need to pass to another project.And it's increase the line of code which i want to reduce it Commented Jun 7, 2017 at 6:56

1 Answer 1

1

Here is something I have implemented that takes an object and reads the property values then formats it as a csv'd string.

The Write.WriteObject() is an example of how you would use it.

public static class StringExtensions
{
    public static string ToCSV(this object field, bool first)
    {
        if(field != null)
        {
            string retVal = field.ToString();
            retVal = retVal.Contains(",") || retVal.Contains("\"") || retVal.Contains("\r\n") 
                ? "\"" + retVal + "\"" : retVal;
            retVal = first ? retVal : "," + retVal;
            return retVal;
        }
        else
        {
            return first ? "" : ",";
        }
    }
}

public class Format
{
    public string CommaSeparatedObject(Type layoutType, object value, bool header)
    {
        bool first = true;

        string retVal = "";
        PropertyInfo[] props = layoutType.GetProperties();

        if (header)
        {
            foreach (PropertyInfo prop in props)
            {
                retVal += prop.Name.ToCSV(first);
                first = false;
            }
        }
        else
        {
            foreach (PropertyInfo prop in props)
            {
                retVal += prop.GetValue(value).ToCSV(first);
                first = false;
            }
        }


        return retVal;
    }
}

public class Write
{
    public void WriteObject(IEnumerable<myClass> myObj)
    {
        StreamWriter sw = new StreamWriter(parameters.OutputFile, true, Encoding.ASCII, 16 * 1024);

        sw.WriteLine(new Format().CommaSeparatedObject(typeof(myClass), item, true))); //write header

        foreach(var item in myObj)
        {
            sw.WriteLine(new Format().CommaSeparatedObject(typeof(myClass), item, false)));
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.