Created
May 24, 2013 06:12
-
-
Save mabster/5641578 to your computer and use it in GitHub Desktop.
A quick response to https://gist.github.com/5640717.git in C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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