0

Hello I have a code like this:

public  string dataToCsv(Dictionary<string, List<string>> data, long qty, string segment)
        {
            string dateToday = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss");
            string user = Environment.UserName;
            var pathToCsv = "C:/Users/" + user + "/Documents/"+ segment + "-" + dateToday + ".csv";
            StringBuilder sb = new StringBuilder();
           
           
             String csv = String.Join(",",
                    data.Select(d => d.Key));
             sb.Append(csv + Environment.NewLine);

            for (int i = 0; i < qty; i++) {
                
                String csv1 = String.Join(",",
                   data.Select(d => string.Join(",", d.Value.Skip(i).Take(1))));
                sb.Append(csv1 + Environment.NewLine);
            }

            System.IO.File.WriteAllText(pathToCsv, sb.ToString());

            return pathToCsv;           

        } 

Works pretty fine but when an element value has a comma. That value is separated and lag the columns of the csv. I need if the element has a comma put with them in the csv. Is there any way to do this?

Maybe if I change the separator from the dictionary but don´t know how to do it. My Dictionary has a list of strings.

dbc
  • 104,963
  • 20
  • 228
  • 340
  • 4
    what standard are you using? typically an element with a comma is enclosed in quotes as it is a string – Hogan Jun 15 '22 at 17:34
  • Does this answer your question? [Dealing with commas in a CSV file](https://stackoverflow.com/questions/769621/dealing-with-commas-in-a-csv-file) – IVSoftware Jun 15 '22 at 18:15

0 Answers0