<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: System.Linq.Enumerable.Aggregate &#8211; Better Know an Extension Method Part 1</title>
	<atom:link href="http://www.bleevo.com/2009/02/system-linq-enumerable-aggregate-better-know-an-extension-method-part-1/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bleevo.com/2009/02/system-linq-enumerable-aggregate-better-know-an-extension-method-part-1/</link>
	<description>Software, Business &#38; Life</description>
	<lastBuildDate>Tue, 20 Apr 2010 20:11:35 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: loopdoop</title>
		<link>http://www.bleevo.com/2009/02/system-linq-enumerable-aggregate-better-know-an-extension-method-part-1/comment-page-1/#comment-1845</link>
		<dc:creator>loopdoop</dc:creator>
		<pubDate>Thu, 03 Dec 2009 15:17:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.bleevo.com/?p=52#comment-1845</guid>
		<description>defeniately bookmarked</description>
		<content:encoded><![CDATA[<p>defeniately bookmarked</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Paul</title>
		<link>http://www.bleevo.com/2009/02/system-linq-enumerable-aggregate-better-know-an-extension-method-part-1/comment-page-1/#comment-1073</link>
		<dc:creator>Paul</dc:creator>
		<pubDate>Thu, 07 May 2009 13:22:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.bleevo.com/?p=52#comment-1073</guid>
		<description>Is this only available in vs 2008 or could I use this functionality in vs 2005 and just include the System.Core?</description>
		<content:encoded><![CDATA[<p>Is this only available in vs 2008 or could I use this functionality in vs 2005 and just include the System.Core?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tristan Smith</title>
		<link>http://www.bleevo.com/2009/02/system-linq-enumerable-aggregate-better-know-an-extension-method-part-1/comment-page-1/#comment-50</link>
		<dc:creator>Tristan Smith</dc:creator>
		<pubDate>Mon, 30 Mar 2009 10:08:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.bleevo.com/?p=52#comment-50</guid>
		<description>It&#039;s surprising just how much of a productivity boost advances like this give us.

Nice tip!</description>
		<content:encoded><![CDATA[<p>It&#8217;s surprising just how much of a productivity boost advances like this give us.</p>
<p>Nice tip!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bart Czernicki</title>
		<link>http://www.bleevo.com/2009/02/system-linq-enumerable-aggregate-better-know-an-extension-method-part-1/comment-page-1/#comment-23</link>
		<dc:creator>Bart Czernicki</dc:creator>
		<pubDate>Fri, 20 Feb 2009 05:21:58 +0000</pubDate>
		<guid isPermaLink="false">http://www.bleevo.com/?p=52#comment-23</guid>
		<description>The aggregate function expression is also part of the F# functional language.  Other places it is useful is when doing things like a weighted average on the fly for records bound in a data grid control or doing statistical calculations or business intelligence calcs.  Back in the .NET 1.x days this was such a pain in the ass (as I didn&#039;t understand delegates back then well enough to be dangerous).</description>
		<content:encoded><![CDATA[<p>The aggregate function expression is also part of the F# functional language.  Other places it is useful is when doing things like a weighted average on the fly for records bound in a data grid control or doing statistical calculations or business intelligence calcs.  Back in the .NET 1.x days this was such a pain in the ass (as I didn&#8217;t understand delegates back then well enough to be dangerous).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Niki</title>
		<link>http://www.bleevo.com/2009/02/system-linq-enumerable-aggregate-better-know-an-extension-method-part-1/comment-page-1/#comment-21</link>
		<dc:creator>Niki</dc:creator>
		<pubDate>Thu, 19 Feb 2009 14:31:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.bleevo.com/?p=52#comment-21</guid>
		<description>Sorry, the part about folds was a little cryptic: What I meant was that Enumerable.Aggregate is equivalent to the operation called &quot;left fold&quot; in functional programming (http://en.wikipedia.org/wiki/Fold_(higher-order_function) ).

In functional programming, folding is often used to build complex data structures like lists: You define a function called &quot;Cons&quot; that builds a linked list, so if nil is the empty list Cons(1, nil) would be the list containing only the element 1, Cons(1, Cons(2, nil)) would be the list containing 1,2 and so on. Then you can do things like (in .NET syntax): 
  myList.Aggregate( (nextElement, list) =&gt; Cons(nextElement, list), nil); 
or shorter:
  myList.Aggregate(Cons, nil); 
Which would just duplicate the list. But you can do any kind of processing in the fold, e.g. you might check for a condition:
  myList.Aggregate( (nextElement, list) =&gt; someCondition(nextElement) ? Cons(nextElement, list) : list, nil); 
which filters a list like &quot;Enumerable.Where&quot;.</description>
		<content:encoded><![CDATA[<p>Sorry, the part about folds was a little cryptic: What I meant was that Enumerable.Aggregate is equivalent to the operation called &#8220;left fold&#8221; in functional programming (<a href="http://en.wikipedia.org/wiki/Fold_(higher-order_function" rel="nofollow">http://en.wikipedia.org/wiki/Fold_(higher-order_function</a>) ).</p>
<p>In functional programming, folding is often used to build complex data structures like lists: You define a function called &#8220;Cons&#8221; that builds a linked list, so if nil is the empty list Cons(1, nil) would be the list containing only the element 1, Cons(1, Cons(2, nil)) would be the list containing 1,2 and so on. Then you can do things like (in .NET syntax):<br />
  myList.Aggregate( (nextElement, list) =&gt; Cons(nextElement, list), nil);<br />
or shorter:<br />
  myList.Aggregate(Cons, nil);<br />
Which would just duplicate the list. But you can do any kind of processing in the fold, e.g. you might check for a condition:<br />
  myList.Aggregate( (nextElement, list) =&gt; someCondition(nextElement) ? Cons(nextElement, list) : list, nil);<br />
which filters a list like &#8220;Enumerable.Where&#8221;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris Hampson</title>
		<link>http://www.bleevo.com/2009/02/system-linq-enumerable-aggregate-better-know-an-extension-method-part-1/comment-page-1/#comment-20</link>
		<dc:creator>Chris Hampson</dc:creator>
		<pubDate>Thu, 19 Feb 2009 12:11:52 +0000</pubDate>
		<guid isPermaLink="false">http://www.bleevo.com/?p=52#comment-20</guid>
		<description>@Niki didnt really think of it in the way of a fold, what do you mean by con?</description>
		<content:encoded><![CDATA[<p>@Niki didnt really think of it in the way of a fold, what do you mean by con?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris Hampson</title>
		<link>http://www.bleevo.com/2009/02/system-linq-enumerable-aggregate-better-know-an-extension-method-part-1/comment-page-1/#comment-19</link>
		<dc:creator>Chris Hampson</dc:creator>
		<pubDate>Thu, 19 Feb 2009 12:10:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.bleevo.com/?p=52#comment-19</guid>
		<description>@J thanks I will look at this some more and update the article.</description>
		<content:encoded><![CDATA[<p>@J thanks I will look at this some more and update the article.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: J</title>
		<link>http://www.bleevo.com/2009/02/system-linq-enumerable-aggregate-better-know-an-extension-method-part-1/comment-page-1/#comment-17</link>
		<dc:creator>J</dc:creator>
		<pubDate>Thu, 19 Feb 2009 11:43:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.bleevo.com/?p=52#comment-17</guid>
		<description>Note that in Test Case 3 you can change the result type in the last overload. So instead of x =&gt; x.Append(&quot;DONE&quot;) you can do x =&gt; x.ToString(), and not have to worry about the string builder object except in the scope of the Aggregate function.

This same technique could be used in the last example to convert the double to decimal.</description>
		<content:encoded><![CDATA[<p>Note that in Test Case 3 you can change the result type in the last overload. So instead of x =&gt; x.Append(&#8220;DONE&#8221;) you can do x =&gt; x.ToString(), and not have to worry about the string builder object except in the scope of the Aggregate function.</p>
<p>This same technique could be used in the last example to convert the double to decimal.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Niki</title>
		<link>http://www.bleevo.com/2009/02/system-linq-enumerable-aggregate-better-know-an-extension-method-part-1/comment-page-1/#comment-15</link>
		<dc:creator>Niki</dc:creator>
		<pubDate>Thu, 19 Feb 2009 10:46:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.bleevo.com/?p=52#comment-15</guid>
		<description>@Chris: There are lots of use cases for Aggregate: The most obvious are of course logical/arithmetic operations. Enumerable does have a Sum extension method, but with Aggregate you can also easily calculate the product of a list of numbers, or the sum of the squares, or an xor-checksum.
(Actually, Aggregate is a lot more general than that: Almost any list processing can be simplified to a fold aka Enumberable.Aggregate. Unfortunately, Linq has no built-in equivalent for Cons, so it&#039;s harder to exploit the general power of Aggregate to build new lists as result.)

BTW: I think you&#039;re not using it the way it&#039;s meant:
        var roundedGrandTotal = orders.Aggregate(new double(), 
            (total, order) =&gt; total += order.Total,
            (total) =&gt; Math.Round(total, 2));

In the second parameter &quot;(total, order) =&gt; total += order.Total&quot;, the &quot;total&quot; parameter of the lambda expression is not a mutable variable. It&#039;s a by-value parameter, and it&#039;s value is lost as soon as it goes out of scope. Your code only works because += has a value. This would be clearer:
        var roundedGrandTotal = orders.Aggregate(new double(), 
            (total, order) =&gt; total + order.Total,
            (total) =&gt; Math.Round(total, 2));</description>
		<content:encoded><![CDATA[<p>@Chris: There are lots of use cases for Aggregate: The most obvious are of course logical/arithmetic operations. Enumerable does have a Sum extension method, but with Aggregate you can also easily calculate the product of a list of numbers, or the sum of the squares, or an xor-checksum.<br />
(Actually, Aggregate is a lot more general than that: Almost any list processing can be simplified to a fold aka Enumberable.Aggregate. Unfortunately, Linq has no built-in equivalent for Cons, so it&#8217;s harder to exploit the general power of Aggregate to build new lists as result.)</p>
<p>BTW: I think you&#8217;re not using it the way it&#8217;s meant:<br />
        var roundedGrandTotal = orders.Aggregate(new double(),<br />
            (total, order) =&gt; total += order.Total,<br />
            (total) =&gt; Math.Round(total, 2));</p>
<p>In the second parameter &#8220;(total, order) =&gt; total += order.Total&#8221;, the &#8220;total&#8221; parameter of the lambda expression is not a mutable variable. It&#8217;s a by-value parameter, and it&#8217;s value is lost as soon as it goes out of scope. Your code only works because += has a value. This would be clearer:<br />
        var roundedGrandTotal = orders.Aggregate(new double(),<br />
            (total, order) =&gt; total + order.Total,<br />
            (total) =&gt; Math.Round(total, 2));</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Neil Bostrom</title>
		<link>http://www.bleevo.com/2009/02/system-linq-enumerable-aggregate-better-know-an-extension-method-part-1/comment-page-1/#comment-14</link>
		<dc:creator>Neil Bostrom</dc:creator>
		<pubDate>Thu, 19 Feb 2009 10:09:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.bleevo.com/?p=52#comment-14</guid>
		<description>Great article, so many hidden gems in those extension methods on IEnumerable.

Keep up the great work!</description>
		<content:encoded><![CDATA[<p>Great article, so many hidden gems in those extension methods on IEnumerable.</p>
<p>Keep up the great work!</p>
]]></content:encoded>
	</item>
</channel>
</rss>
