Translating this sample into Visual Basic 9.0 produces almost identical code.
However, this translation is a bit naive because Visual Basic 9.0 actually provides syntax for more of the standard query operators than C# 3.0 does. While we have to call the "Sum" query operator explicitly in C#, Visual Basic allows us to use it directly in the query.
In fact, Visual Basic even allows us to create our own aggregate functions and use them directly in query expressions.
Here we get the product of the even numbers in the array. (I removed the expression to square each even number because it produced an OverflowException.)
I should point out that there is a behavioral difference that the Visual Basic "Aggregate" keyword introduces. A standard "From" query expression is delay evaluated. That is, the results aren't actually evaluated until they are accessed through, say, a "For Each" loop. However, an "Aggregate" query expression forces the results to be evaluated immediately. In contrast, C# 3.0 query expressions always produce results that are delay evaluated.1
1A bold statement that will be completely recanted if any reader can find an example that proves otherwise.2 2Please, prove me wrong. Seriously. I'm interested in this stuff.3 3This footnote motif is clearly ripped off from Raymond Chen.
Page rendered at Sunday, September 07, 2008 10:15:52 PM (Eastern Standard Time, UTC-05:00)