<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Lambdas: Aggregating Strings</title><link>http://msmvps.com/blogs/deborahk/archive/2009/07/03/lambdas-aggregating-strings.aspx</link><description>Looping through a list to append strings is often more challenging than it should be. For example… In C#: string emailAddresses=string.Empty; foreach (Customer c in custList) { &amp;#160;&amp;#160;&amp;#160; emailAddresses += c.EmailAddress + &amp;quot;;&amp;quot;; } In</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Lambda Expressions: An Introduction</title><link>http://msmvps.com/blogs/deborahk/archive/2009/07/03/lambdas-aggregating-strings.aspx#1731728</link><pubDate>Sun, 11 Oct 2009 23:57:55 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1731728</guid><dc:creator>Deborah's Developer MindScape</dc:creator><description>&lt;p&gt;My presentation at our local Code Camp was fun. I covered a lot of material and had a GREAT audience&lt;/p&gt;
&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1731728" width="1" height="1"&gt;</description></item><item><title>re: Lambdas: Aggregating Strings</title><link>http://msmvps.com/blogs/deborahk/archive/2009/07/03/lambdas-aggregating-strings.aspx#1698449</link><pubDate>Mon, 06 Jul 2009 17:46:15 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1698449</guid><dc:creator>Deborah Kurata</dc:creator><description>&lt;p&gt;Hi Jonathan -&lt;/p&gt;
&lt;p&gt;Thanks for your suggestion.&lt;/p&gt;
&lt;p&gt;It appears that Join is smarter and does not create the excess strings that the Aggregate method creates.&lt;/p&gt;
&lt;p&gt;Cool! Thanks again!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1698449" width="1" height="1"&gt;</description></item><item><title>re: Lambdas: Aggregating Strings</title><link>http://msmvps.com/blogs/deborahk/archive/2009/07/03/lambdas-aggregating-strings.aspx#1698447</link><pubDate>Mon, 06 Jul 2009 17:39:04 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1698447</guid><dc:creator>Deborah Kurata</dc:creator><description>&lt;p&gt;Hey Shawn -&lt;/p&gt;
&lt;p&gt;Thanks for stopping by my blog.&lt;/p&gt;
&lt;p&gt;Yes, just because you can do something does not necessarily mean that you should. Have you seen the scuba diving cat? :-)&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://www.youtube.com/watch?v=bScOj4e0bOQ"&gt;www.youtube.com/watch&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;As developers, we always have to evaluate techniques for &amp;quot;fitness of purpose&amp;quot;. We have to decide whether a specific technique is appropriate for what our application needs to achieve.&lt;/p&gt;
&lt;p&gt;This technique may be fine for an application that will have 1 to 5 email addresses. But it would not work well for a spamming app sending to hundreds of email addresses.&lt;/p&gt;
&lt;p&gt;Thanks for your thoughts!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1698447" width="1" height="1"&gt;</description></item><item><title>Lamdas </title><link>http://msmvps.com/blogs/deborahk/archive/2009/07/03/lambdas-aggregating-strings.aspx#1698418</link><pubDate>Mon, 06 Jul 2009 15:24:56 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1698418</guid><dc:creator>Greg</dc:creator><description>&lt;p&gt;It&amp;#39;s easier to visualy inspect and verify that the for loop exmple works than the lambda based one.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1698418" width="1" height="1"&gt;</description></item><item><title>re: Lambdas: Aggregating Strings</title><link>http://msmvps.com/blogs/deborahk/archive/2009/07/03/lambdas-aggregating-strings.aspx#1698092</link><pubDate>Sun, 05 Jul 2009 03:03:45 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1698092</guid><dc:creator>Jonathan</dc:creator><description>&lt;p&gt;I prefer using string.join to accomplish these kinds of things -- the only downside is that it needs a string[]. &amp;nbsp;Luckily, LINQ includes ToArray() to turn the IEnumerable&amp;lt;string&amp;gt; that comes out of Select into a string[].&lt;/p&gt;
&lt;p&gt;string email = string.join(&amp;quot;; &amp;quot;,custList.Select(c =&amp;gt; c.EmailAddress).ToArray());&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1698092" width="1" height="1"&gt;</description></item><item><title>re: Lambdas: Aggregating Strings</title><link>http://msmvps.com/blogs/deborahk/archive/2009/07/03/lambdas-aggregating-strings.aspx#1697739</link><pubDate>Fri, 03 Jul 2009 17:54:22 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1697739</guid><dc:creator>Shawn Wildermuth</dc:creator><description>&lt;p&gt;The other problem here is that the aggregation of the strings is cool, but the string + string approach involves creation of a lot of strings for the GC to clear. &amp;nbsp;You should be usng a StringBuilder. &amp;nbsp;This is very inefficient on a large number of strings:&lt;/p&gt;
&lt;p&gt;string final = &amp;quot;&amp;quot;;&lt;/p&gt;
&lt;p&gt;for (var x = 0; x &amp;lt; 10; ++x)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp;final += x.ToString();&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;This simple code (like your aggregation) will create about 30 strings that aren&amp;#39;t used. &amp;nbsp;Being mindful of the immutability of strings is important.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1697739" width="1" height="1"&gt;</description></item></channel></rss>