We help IT Professionals succeed at work.
Get Started

C# LINQ - Extract Values of Dictionary<string,string> as CSV

nightshadz asked
Last Modified: 2015-06-29
I'm trying to output the values of the Fields dictionary (Dictionary<string,string>) as a CSV file. I need to put quotes around each value but when I use...

var values = string.Join(",", "\"" + a.Fields.Select(v => v.Value) + "\"");

...it outputs as


However, when I remove the quotations and use...

var values = string.Join(",", a.Fields.Select(v => v.Value));

...it outputs fine, but I need quotes around the values because some of the values contain commas.

        public void Print()
            var csv = new StringBuilder();            
            var header = String.Join(",", audit[0].Fields.Select(k => k.Key));

            foreach (var a in audit)
                var values = string.Join(",", "\"" + a.Fields.Select(v => v.Value) + "\"");
                csv.Append(Environment.NewLine + values);

            File.WriteAllText(AppConfig.CSVFilePath(), csv.ToString());

Open in new window

Watch Question
Distinguished Expert 2017
This problem has been solved!
Unlock 1 Answer and 7 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE