<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.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>LINQ Farm: More on the LINQ Aggregate Operators</title><link>http://blogs.msdn.com/charlie/archive/2008/07/24/linq-farm-more-on-aggregate-operators.aspx</link><description>The LINQ aggregate operators allow you to perform simple math operations over the elements in a sequence. This post is designed to walk you through those operators, and give you an overview of how to use them. Table 1 shows a list of the 7 aggregate operators.</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>LINQ Farm: More on the LINQ Aggregate Operators</title><link>http://blogs.msdn.com/charlie/archive/2008/07/24/linq-farm-more-on-aggregate-operators.aspx#8770692</link><pubDate>Fri, 25 Jul 2008 03:54:11 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8770692</guid><dc:creator>DotNetKicks.com</dc:creator><description>&lt;p&gt;You've been kicked (a good thing) - Trackback from DotNetKicks.com&lt;/p&gt;
</description></item><item><title>Clarifying the semantics of ICollection&lt;T&gt;.Count</title><link>http://blogs.msdn.com/charlie/archive/2008/07/24/linq-farm-more-on-aggregate-operators.aspx#8772329</link><pubDate>Fri, 25 Jul 2008 18:08:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8772329</guid><dc:creator>Jonathan Pryor</dc:creator><description>&lt;p&gt;Would it be possible to clarify the semantics of ICollection&amp;lt;T&amp;gt;.Count for collections with more than int.MaxValue elements? &amp;nbsp;If we required that ICollection&amp;lt;T&amp;gt;.Count throw an InvalidOperationException or something if the collection size was too large, then the Enumerate.LongCount() extension method could rely on ICollection&amp;lt;T&amp;gt;.Count for collections with fewer than int.MaxValue elements, e.g.:&lt;/p&gt;
&lt;p&gt;public static long LongCount&amp;lt;T&amp;gt; (this IEnumerable&amp;lt;T&amp;gt; source)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp;ICollection&amp;lt;T&amp;gt; c = source as ICollection&amp;lt;T&amp;gt;;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;if (c != null) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;try { return c.Count; }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;catch { /* ignore; manually count elements */ }&lt;/p&gt;
&lt;p&gt; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp;long n = 0;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;foreach (var e in source) ++n;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;return n;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;(Note: the above misses a potential optimization for when `source` is a T[], in which Array.LongLength can be used directly; it is merely provided for discussion purposes.)&lt;/p&gt;
&lt;p&gt;Not only could this vastly increase the performance of LongCount() in the majority of cases (how often do you have collections with more than int.MaxValue elements?), but it's already possible _now_ for collection types to have more than int.MaxValue items -- LinkedList&amp;lt;T&amp;gt; should be able to store as many items as memory permits (which is quite a bit on 64-bit platforms), yet it also provides ICollection&amp;lt;T&amp;gt;.Count. &amp;nbsp;What should ICollection&amp;lt;T&amp;gt;.Count do when it has more than int.MaxValue items?&lt;/p&gt;
&lt;p&gt;I can't speak for .NET here, but I know that Mono's LinkedList&amp;lt;T&amp;gt; source will in fact wrap around if you add more int.MaxValue+1 items, so ICollection&amp;lt;T&amp;gt;.Count will return int.MinValue, which can't be good/sensible/sane for any client code. &amp;nbsp;(Even more bizarrely, if you add uint.MaxValue+1 items, .Count will return 0 -- the joys of integer math.)&lt;/p&gt;
</description></item><item><title>Fixing the LINQ samples</title><link>http://blogs.msdn.com/charlie/archive/2008/07/24/linq-farm-more-on-aggregate-operators.aspx#8791602</link><pubDate>Wed, 30 Jul 2008 16:51:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8791602</guid><dc:creator>Valkyrie-MT</dc:creator><description>&lt;p&gt;Charlie, this is a bit off topic, but I saw that you used Enumerable.Range and it reminded me of my first encounter with this and how frustrated I was trying to get the example from the LINQ 101 samples to work because they use the old Sequence.Range instead of Enumerable.Range. &amp;nbsp;Can you ask someone to fix it because it is still out of date...&lt;/p&gt;
&lt;p&gt;Thanks! &amp;nbsp;&lt;/p&gt;
</description></item><item><title>re: LINQ Farm: More on the LINQ Aggregate Operators</title><link>http://blogs.msdn.com/charlie/archive/2008/07/24/linq-farm-more-on-aggregate-operators.aspx#8803705</link><pubDate>Sat, 02 Aug 2008 05:18:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8803705</guid><dc:creator>pkellner</dc:creator><description>&lt;p&gt;Nice article. &amp;nbsp;Great to see aggregate's explained so clearly and with good examples.&lt;/p&gt;
</description></item><item><title>Know When to Fold</title><link>http://blogs.msdn.com/charlie/archive/2008/07/24/linq-farm-more-on-aggregate-operators.aspx#8977430</link><pubDate>Mon, 06 Oct 2008 06:26:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8977430</guid><dc:creator>Cay Horstmann's Blog</dc:creator><description>&lt;p&gt;I demonstrate the usefulness of the nifty &amp;quot;fold&amp;quot; operator in Scala and ruminate on functional programming support in blue collar languages.&lt;/p&gt;
</description></item><item><title>re: LINQ Farm: More on the LINQ Aggregate Operators</title><link>http://blogs.msdn.com/charlie/archive/2008/07/24/linq-farm-more-on-aggregate-operators.aspx#9914789</link><pubDate>Thu, 29 Oct 2009 16:27:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9914789</guid><dc:creator>Sam</dc:creator><description>&lt;p&gt;Given a list of items, you showed how to get a min value with LE as follows.&lt;/p&gt;
&lt;p&gt;items.Min(l =&amp;gt; l.Length).&lt;/p&gt;
&lt;p&gt;This is good so far. &lt;/p&gt;
&lt;p&gt;But how do we use LINQ and LE to get THE ITEM with min length and not the value of min length as you showed? &lt;/p&gt;
&lt;p&gt;Basically how do you fill in the question marks here?&lt;/p&gt;
&lt;p&gt;Item lItemWithMinLength = items.???&lt;/p&gt;
&lt;p&gt;Thanks! I'd appreciate it your answer on this one.&lt;/p&gt;
</description></item><item><title>re: LINQ Farm: More on the LINQ Aggregate Operators</title><link>http://blogs.msdn.com/charlie/archive/2008/07/24/linq-farm-more-on-aggregate-operators.aspx#9921174</link><pubDate>Thu, 12 Nov 2009 04:29:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9921174</guid><dc:creator>John Lin</dc:creator><description>&lt;p&gt;Try this:&lt;/p&gt;
&lt;p&gt;items.Aggregate((a,b) =&amp;gt; a.Length &amp;lt; b.Length? a : b);&lt;/p&gt;
</description></item></channel></rss>