Tech Off Post

Single Post Permalink

View Thread: What's the functional programming / Linq form of this?
  • User profile image
    amotif

    amotif wrote:
    Anyone know how to format this to "5=>a,e" etc. in a linq-esque fashion?


    Okay, I think I've settled on this:

        var result = from pair in data
                     group pair by pair.Value into g
                     select g.FormatReverseMapping();
    
        foreach (string s in result)
            Console.WriteLine(s);
    


    with an extension method:

        internal static string FormatReverseMapping(this IGrouping> grouping)
        {
            StringBuilder buffer = new StringBuilder();
    
            buffer.Append(grouping.Key);
            buffer.Append("=>");
            foreach (var x in grouping)
                buffer.Append(x.Key);
    
            return buffer.ToString();
        }
    

    Smiley

    (psst-- take a look at Systems.Collections.Generic.KeyValuePair.)