Skip to content

Instantly share code, notes, and snippets.

@mabster
Created May 24, 2013 06:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mabster/5641578 to your computer and use it in GitHub Desktop.
Save mabster/5641578 to your computer and use it in GitHub Desktop.
A quick response to https://gist.github.com/5640717.git in C#
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var responses = new[]
{
new SurveyResponse
{
Site = "site1",
Results = {
{ProgrammingLanguage.CSharp, 3},
{ProgrammingLanguage.FSharp, 1},
{ProgrammingLanguage.Haskell, 0},
}
},
new SurveyResponse
{
Site = "site2",
Results = {
{ProgrammingLanguage.CSharp, 3},
{ProgrammingLanguage.Ruby, 5},
}
},
new SurveyResponse
{
Site = "site3",
Results = {
{ProgrammingLanguage.Ruby, 7},
{ProgrammingLanguage.JavaScript, 4},
}
},
};
Console.WriteLine(SurveyToCsv(responses));
}
public static string SurveyToCsv(IEnumerable<SurveyResponse> responses)
{
return
"site," + String.Join(",", Enum.GetNames(typeof(ProgrammingLanguage))) + "\n"
+ String.Join("\n",
responses.Select(response =>
response.Site + ","
+ String.Join(",",
Enum.GetValues(typeof(ProgrammingLanguage))
.Cast<ProgrammingLanguage>()
.Select(p => (response.Results.ContainsKey(p) ? response.Results[p] : 0).ToString())
)));
}
}
class SurveyResponse
{
public SurveyResponse()
{
Results = new Dictionary<ProgrammingLanguage, int>();
}
public string Site;
public Dictionary<ProgrammingLanguage, int> Results;
}
enum ProgrammingLanguage
{
CSharp,
FSharp,
Haskell,
Ruby,
JavaScript
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment