Skip to content

Different code, same result

July 28, 2008

Do you ever get hung up on finding the best way to perform an ostensibly simple operation?  I do.  While helping someone out with a LINQ query I wanted to dump the test output to a message box to check the results.

var foo = from app in db.Applications
                         where !app.ApplicationActive
                         select app.ApplicationName;
               
MessageBox.Show(string.Concat(foo.ToArray()));

This works but it produces a kinda crappy box with the values all stuck together.

CropperCapture[4]

So how can I, without adding another string or StringBuilder and some sort of loop, insert some newlines or any sort of delimiter in between the results. 

My first idea was to use an extension method, like so:

MessageBox.Show(foo.ToArray().ToNewLineStrings());

public static string ToNewLineStrings(this Array foo)
  {
      string boo = "";
      foreach (string loo in foo)
          boo += loo + "\r\n";
      return boo;
  }

This isn’t really what I wanted to accomplish though.  But its a neat example of an extension method though.

I was still enjoying the extension methods so I tried one more

MessageBox.Show(string.Concat(foo.ToArray().AddNewLines().ToArray()));

public static IEnumerable<string> AddNewLines(this Array foo)
 {
     foreach (string boo in foo)
         yield return boo + "\r\n";
 }

Ok, enough playing around, lets try some lambdas.

MessageBox.Show(string.Concat(foo.ToList().Select(goo => goo += "\r\n").ToArray()));

If I could come up with a way to avoid all the casting, I’d say this was my cleanest solution to the problem.  Not unlike saying SELECT tablename.Name + ‘\r\n’ FROM …  in SQL.

Finally, I was browsing Charlie Calvert’s blog, always a good source for clarifying language features, and saw that the Aggregate method fit my needs perfectly.

MessageBox.Show(foo.ToArray().Aggregate((a, b) => (a + "\r\n" + b)));

The Aggregate essentially runs through your collection with a function that offers the first and second values in the collection on the first pass and thereafter the product of the previous pass and the next value.  So if our collection had the values foo, too, boo, and yoo the logic would look like this:

Pass 1:

foo + "\r\n" + too

Pass 2:

foo\r\ntoo + \r\n + boo

Pass 3:

foo\r\n\too\r\nboo + \r\n + yoo

So after all that we have 4 different ways to produce:

CropperCapture[2]

var foo = from app in db.Applications
            where !app.ApplicationActive
            select app.ApplicationName;
                
MessageBox.Show(string.Concat(foo.ToArray()));
                                
MessageBox.Show(foo.ToArray().ToNewLineStrings());
MessageBox.Show(string.Concat(foo.ToArray().AddNewLines().ToArray()));
MessageBox.Show(string.Concat(foo.ToList().Select(ggg => ggg += "\r\n").ToArray()));
MessageBox.Show(foo.ToArray().Aggregate("",(a, b) => (a + "\r\n" + b)));

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: