MSDN Magazine - July 2008 - (Page 37) results each time, but you should see a list like the following each time you execute the code: 6, 8, 7, 1, 2, 9, 10, 4, 3, 5 10. 11. 12. 13. Liz Nixon Liu Wong Helvetius Nagy Karl Jablonski The Enumerable.Reverse method returns its input sequence in reverse order. The following code calls the Reverse method both as a Shared method of the Enumerable class and as an instance method on the list resulting from calling the Enumerable.Range method: 'From ReverseDemo in the sample: Dim items = Enumerable.Range(1, 10) Dim reversed = items.Reverse() reversed = Enumerable.Reverse(items) However you call the Reverse method, you’ll end up with the following list: 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 The Enumerable.SelectMany method takes a collection of items, each of which can be a collection of items, and collapses the twodimensional input into a single-dimensional output sequence. This method can be useful if you need to take, for example, a list of categories, each of which contains products, and end up with a list of products with corresponding category information. The following example retrieves a list of categories for which the category contains less than seven products, and projects the result into a single list containing information about each product, along with category information: ' From SelectManyDemo in the sample: Dim db As New SimpleDataContext Dim categories = _ From cat In db.Categories Where cat.Products.Count < 7 Dim manyResults As IEnumerable(Of String) = _ categories.SelectMany(Function(cat, index) _ cat.Products.Select( _ Function(prod As Product) String.Format("{0}. Category {1}: {2}", _ index, prod.CategoryID, prod.ProductName))) The Enumerable.Repeat method creates a list containing the value you supply, repeated as many times as you specify: 'From RepeatDemo in the sample: Dim repeated = Enumerable.Repeat("Hello", 5) After calling the previous code, the repeated variable contains the following items: Hello, Hello, Hello, Hello, Hello Use the Enumerable.Empty method to create an empty IEnumerable(Of T) that’s set up to contain the specified data type. Using this method, you could create the empty instance of the collection and add items to it as necessary. By initializing the collection to an empty collection, you never have to worry about whether the reference is Nothing: 'From EmptyDemo in the sample: Dim emptyList = _ Enumerable.Empty(Of Customer)() In this case, the lambda expression passed to the SelectMany method accepts a reference to a specific category, working through each category in turn. This particular overload of the SelectMany method passes the index of the category along with the category to the lambda expression. Given the category and its index, the lambda expression calls the Select method on the Products prop- Selecting Sequences The Enumerable class provides two different extension methods that allow you to select a sequence of elements, projecting the elements from the input sequence into an output sequence. The Enumerable.Select method simply projects each element into a new form. The following example takes a sequence containing customers in the U.S. from the Northwind Customers table, then projects the results into a sequence that contains a numbered list of contact names: ' From SelectDemo in the sample: Dim db As New SimpleDataContext Dim results = _ From cust In db.Customers _ Where cust.Country = "USA" Dim selectResults = results.Select( _ Function(cust, index) _ String.Format("{0}. {1}", _ index + 1, cust.ContactName)) Note that the Select method has multiple overloaded versions. After running this code, selectResults contains a list of values resembling the following: 1. 2. 3. 4. 5. 6. 7. 8. 9. Howard Snyder Yoshi Latimer John Steel Jaime Yorres Fran Wilson Rene Phillips Paula Wilson Jose Pavarotti Art Braunschweiger msdnmagazine.com July 2008 37 http://www.ektron.com/msdn http://www.ektron.com/msdn http://msdnmagazine.com
Table of Contents Feed for the Digital Edition of MSDN Magazine - July 2008 MSDN Magazine - July 2008 Contents Toolbox CLR Inside Out Flex Your Data Data Points Advanced Basics Office Space Cutting Edge Data Services ADO.NET Data and WPF Transactions WCF P2P Test Run Security Briefs Foundations .NET Matters {End Bracket} MSDN Magazine - July 2008 MSDN Magazine - July 2008 - (Page Intro) MSDN Magazine - July 2008 - Contents (Page Cover1) MSDN Magazine - July 2008 - Contents (Page Cover2) MSDN Magazine - July 2008 - Contents (Page 1) MSDN Magazine - July 2008 - Contents (Page 2) MSDN Magazine - July 2008 - Contents (Page 3) MSDN Magazine - July 2008 - Contents (Page 4) MSDN Magazine - July 2008 - Contents (Page 5) MSDN Magazine - July 2008 - Contents (Page 6) MSDN Magazine - July 2008 - Contents (Page 7) MSDN Magazine - July 2008 - Contents (Page 8) MSDN Magazine - July 2008 - Contents (Page 9) MSDN Magazine - July 2008 - Contents (Page 10) MSDN Magazine - July 2008 - Toolbox (Page 11) MSDN Magazine - July 2008 - Toolbox (Page 12) MSDN Magazine - July 2008 - Toolbox (Page 13) MSDN Magazine - July 2008 - Toolbox (Page 14) MSDN Magazine - July 2008 - Toolbox (Page 15) MSDN Magazine - July 2008 - Toolbox (Page 16) MSDN Magazine - July 2008 - CLR Inside Out (Page 17) MSDN Magazine - July 2008 - CLR Inside Out (Page 18) MSDN Magazine - July 2008 - CLR Inside Out (Page 19) MSDN Magazine - July 2008 - CLR Inside Out (Page 20) MSDN Magazine - July 2008 - CLR Inside Out (Page 21) MSDN Magazine - July 2008 - CLR Inside Out (Page 22) MSDN Magazine - July 2008 - CLR Inside Out (Page 23) MSDN Magazine - July 2008 - CLR Inside Out (Page 24) MSDN Magazine - July 2008 - Data Points (Page 25) MSDN Magazine - July 2008 - Data Points (Page 26) MSDN Magazine - July 2008 - Data Points (Page 27) MSDN Magazine - July 2008 - Data Points (Page 28) MSDN Magazine - July 2008 - Data Points (Page 29) MSDN Magazine - July 2008 - Data Points (Page 30) MSDN Magazine - July 2008 - Data Points (Page 31) MSDN Magazine - July 2008 - Data Points (Page 32) MSDN Magazine - July 2008 - Data Points (Page 33) MSDN Magazine - July 2008 - Data Points (Page 34) MSDN Magazine - July 2008 - Advanced Basics (Page 35) MSDN Magazine - July 2008 - Advanced Basics (Page 36) MSDN Magazine - July 2008 - Advanced Basics (Page 37) MSDN Magazine - July 2008 - Advanced Basics (Page 38) MSDN Magazine - July 2008 - Advanced Basics (Page 39) MSDN Magazine - July 2008 - Advanced Basics (Page 40) MSDN Magazine - July 2008 - Advanced Basics (Page 41) MSDN Magazine - July 2008 - Advanced Basics (Page 42) MSDN Magazine - July 2008 - Office Space (Page 43) MSDN Magazine - July 2008 - Office Space (Page 44) MSDN Magazine - July 2008 - Office Space (Page 45) MSDN Magazine - July 2008 - Office Space (Page 46) MSDN Magazine - July 2008 - Office Space (Page 47) MSDN Magazine - July 2008 - Office Space (Page 48) MSDN Magazine - July 2008 - Office Space (Page 49) MSDN Magazine - July 2008 - Office Space (Page 50) MSDN Magazine - July 2008 - Cutting Edge (Page 51) MSDN Magazine - July 2008 - Cutting Edge (Page 52) MSDN Magazine - July 2008 - Cutting Edge (Page 53) MSDN Magazine - July 2008 - Cutting Edge (Page 54) MSDN Magazine - July 2008 - Cutting Edge (Page 55) MSDN Magazine - July 2008 - Cutting Edge (Page 56) MSDN Magazine - July 2008 - Cutting Edge (Page 57) MSDN Magazine - July 2008 - Data Services (Page 58) MSDN Magazine - July 2008 - Data Services (Page 59) MSDN Magazine - July 2008 - Data Services (Page 60) MSDN Magazine - July 2008 - Data Services (Page 61) MSDN Magazine - July 2008 - Data Services (Page 62) MSDN Magazine - July 2008 - Data Services (Page 63) MSDN Magazine - July 2008 - Data Services (Page 64) MSDN Magazine - July 2008 - Data Services (Page 65) MSDN Magazine - July 2008 - Data Services (Page 66) MSDN Magazine - July 2008 - Data Services (Page 67) MSDN Magazine - July 2008 - Data Services (Page 68) MSDN Magazine - July 2008 - Data Services (Page 69) MSDN Magazine - July 2008 - ADO.NET (Page 70) MSDN Magazine - July 2008 - ADO.NET (Page 71) MSDN Magazine - July 2008 - ADO.NET (Page 72) MSDN Magazine - July 2008 - ADO.NET (Page 73) MSDN Magazine - July 2008 - ADO.NET (Page 74) MSDN Magazine - July 2008 - ADO.NET (Page 75) MSDN Magazine - July 2008 - ADO.NET (Page 76) MSDN Magazine - July 2008 - ADO.NET (Page 77) MSDN Magazine - July 2008 - Data and WPF (Page 78) MSDN Magazine - July 2008 - Data and WPF (Page 79) MSDN Magazine - July 2008 - Data and WPF (Page 80) MSDN Magazine - July 2008 - Data and WPF (Page 81) MSDN Magazine - July 2008 - Data and WPF (Page 82) MSDN Magazine - July 2008 - Data and WPF (Page 83) MSDN Magazine - July 2008 - Data and WPF (Page 84) MSDN Magazine - July 2008 - Data and WPF (Page 85) MSDN Magazine - July 2008 - Data and WPF (Page 86) MSDN Magazine - July 2008 - Data and WPF (Page 87) MSDN Magazine - July 2008 - Data and WPF (Page 88) MSDN Magazine - July 2008 - Data and WPF (Page 89) MSDN Magazine - July 2008 - Data and WPF (Page 90) MSDN Magazine - July 2008 - Transactions (Page 91) MSDN Magazine - July 2008 - Transactions (Page 92) MSDN Magazine - July 2008 - Transactions (Page 93) MSDN Magazine - July 2008 - Transactions (Page 94) MSDN Magazine - July 2008 - Transactions (Page 95) MSDN Magazine - July 2008 - Transactions (Page 96) MSDN Magazine - July 2008 - Transactions (Page 97) MSDN Magazine - July 2008 - Transactions (Page 98) MSDN Magazine - July 2008 - Transactions (Page 99) MSDN Magazine - July 2008 - Transactions (Page 100) MSDN Magazine - July 2008 - Transactions (Page 101) MSDN Magazine - July 2008 - Transactions (Page 102) MSDN Magazine - July 2008 - Transactions (Page 103) MSDN Magazine - July 2008 - Transactions (Page 104) MSDN Magazine - July 2008 - WCF P2P (Page 105) MSDN Magazine - July 2008 - WCF P2P (Page 106) MSDN Magazine - July 2008 - WCF P2P (Page 107) MSDN Magazine - July 2008 - WCF P2P (Page 108) MSDN Magazine - July 2008 - WCF P2P (Page 109) MSDN Magazine - July 2008 - WCF P2P (Page 110) MSDN Magazine - July 2008 - Test Run (Page 111) MSDN Magazine - July 2008 - Test Run (Page 112) MSDN Magazine - July 2008 - Test Run (Page 113) MSDN Magazine - July 2008 - Test Run (Page 114) MSDN Magazine - July 2008 - Test Run (Page 115) MSDN Magazine - July 2008 - Test Run (Page 116) MSDN Magazine - July 2008 - Security Briefs (Page 117) MSDN Magazine - July 2008 - Security Briefs (Page 118) MSDN Magazine - July 2008 - Security Briefs (Page 119) MSDN Magazine - July 2008 - Security Briefs (Page 120) MSDN Magazine - July 2008 - Security Briefs (Page 121) MSDN Magazine - July 2008 - Security Briefs (Page 122) MSDN Magazine - July 2008 - Foundations (Page 123) MSDN Magazine - July 2008 - Foundations (Page 124) MSDN Magazine - July 2008 - Foundations (Page 125) MSDN Magazine - July 2008 - Foundations (Page 126) MSDN Magazine - July 2008 - Foundations (Page 127) MSDN Magazine - July 2008 - Foundations (Page 128) MSDN Magazine - July 2008 - Foundations (Page 129) MSDN Magazine - July 2008 - Foundations (Page 130) MSDN Magazine - July 2008 - .NET Matters (Page 131) MSDN Magazine - July 2008 - .NET Matters (Page 132) MSDN Magazine - July 2008 - .NET Matters (Page 133) MSDN Magazine - July 2008 - .NET Matters (Page 134) MSDN Magazine - July 2008 - .NET Matters (Page 135) MSDN Magazine - July 2008 - {End Bracket} (Page 136) MSDN Magazine - July 2008 - {End Bracket} (Page Cover3) MSDN Magazine - July 2008 - {End Bracket} (Page Cover4)
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.