the following function will take the list of dictionary items to a comma separated csv string. public string ConvertToCSV(List<Dictionary<string, string>> items) { if (!items.Any()) return string.Empty; StringBuilder writer = new StringBuilder(); // Generating Header. List<string> headers = items[0].Keys.Select(x => x).OrderBy(x => x).ToList(); writer.AppendLine(string.Join(", ", headers.Select(h => h))); // Generating content. foreach (var item in items) writer.AppendLine(string.Join(", ", headers.Select(h => item[h]))); return writer.ToString(); }
Blog for sharing my programming experience and share my codes