MSDN Magazine - July 2008 - (Page 40) ' From DistinctDemo in the sample: Dim db As New SimpleDataContext Dim countries As IEnumerable(Of String) = _ db.Customers _ .Select(Function(cust) cust.Country) _ .Distinct() countries = customers _ .Distinct(New CustomerCountryComparer) _ .Select(Function(cust) cust.Country) a sequence of customers and then applies ordering using several of these methods: ' From OrderByDemo in the sample: Dim db As New SimpleDataContext Dim customers = _ From cust In db.Customers _ Where cust.ContactTitle = "Owner" ' Sort first by country, then by city descending, then by name: Dim results = _ customers _ .OrderBy(Function(cust) cust.Country) _ .ThenByDescending(Function(cust As Customer) cust.City) _ .ThenBy(Function(cust As Customer) cust.ContactName) _ .Select(Function(cust) String.Format("({0}, {1}) {2}", _ cust.Country, cust.City, cust.ContactName)) You may need to handle empty sequences gracefully, and the Enumerable.DefaultIfEmpty method returns either the entire in put sequence or a defaultvalued instance if the sequence is empty. Using this method, you’re guaranteed that you always have at least a single item of the sequence’s type. The procedure in Figure 4 demonstrates one use for the Default IfEmpty method. This code fills the noCustomers variable with an empty list (there are no customers whose country is “XXX”), then creates the noCustomers1 list calling the DefaultIfEmpty method. Running the sample code produces the following output: noCustomers contains 0 element(s). noCustomers1 contains 1 element(s). XXXXX noCustomers1 contains one element (a Customer with no informa tion filled in). You can call the DefaultIfEmpty method supplying a default value for the singleton item, as in the second method call. Note that Visual Basic won’t complain if you don’t specify the data types for the parameter to the lambda expression in the ThenBy and ThenByDescending method calls, but if you don’t, you won’t get any IntelliSense® to help you. Only the first ordering method can infer the data type of the lambda expression parameter. Odd ly enough, the Select method correctly infers the type, even at the end of the long chained list of methods. The sample sorts the cus tomers whose title is “Owner” first by country, then descending by city, and, finally, by contact name within each city: (Denmark, Kobenhavn) Jytte Petersen (France, Paris) Marie Bertrand (France, Nantes) Janine Labrune (France, Marseille) Laurence Lebihan (Germany, Köln) Henriette Pfalzheim (Mexico, México D.F.) Ana Trujillo (Mexico, México D.F.) Antonio Moreno (Mexico, México D.F.) Miguel Angel Paolino (Norway, Stavern) Jonas Bergulfsen (Poland, Warszawa) Zbyszek Piestrzeniewicz Ordering Sequences You can use Enumerable.OrderBy, Enumerable.ThenBy, Enumer able.OrderByDescending, and Enumerable.ThenByDescending to supply an initial ordering and then one or more secondary order ings (in ascending or descending order). This sample starts with Figure 5 Finding a Sequence ' From AnyDemo in sample: Dim db As New SimpleDataContext Dim results = _ From product In db.Products _ Where product.CategoryID = 1 ' Determine if a list has any elements: Dim anyElements = results.Any() ' Call as extension method or shared method ' of IEnumerable: Dim matchingElements = _ results.Any(Function(prod) prod.ProductName.StartsWith("M")) matchingElements = _ Enumerable.Any(results, _ Function(prod As Product) prod.ProductName.StartsWith("M")) Verifying Sequences Figure 6 Finding Items in a Sequence ' From ContainsDemo in the sample: ' Simple check to see if list contains a value: Dim numbers = Enumerable.Range(1, 10) Dim contains5 = numbers.Contains(5) ' More complex objects require more complex comparison: Dim db As New SimpleDataContext Dim results = _ From product In db.Products _ Where product.CategoryID = 1 Dim item As New Product _ With {.ProductID = 1, .ProductName = "Chai"} Dim containsChai = results.Contains(item, New ProductComparer()) Some applications require you to verify that specific conditions are met within a sequence. Do any of the elements meet a criterion? Do all the elements meet a criterion? Does a particular item appear within the sequence? Are two sequences equal? The Enumerable class provides methods that provide all this information. To determine whether a sequence contains any elements at all, call the Enumerable.Any method without any parameters. To sup ply a condition and to determine whether any elements in the se quence match the condition, pass a function to the method. The method returns True if any elements of the input sequence exist or match the supplied condition (see Figure 5). To determine whether all members of a sequence meet a criterion, call the Enumerable.All method, supplying a function that specifies the condition. This example determines if all the elements in the results collection have a UnitsInStock value greater than 5: ' From AllDemo in sample: ' Is it true that all products have more than 5 units in stock? Dim db As New SimpleDataContext Dim results = _ From product In db.Products _ Where product.CategoryID = 1 Dim allElements = _ results.All(Function(prod) CBool(prod.UnitsInStock > 5)) To determine whether a sequence contains a particular element, call Enumerable.Contains. If you’re searching for a simple value, like a string or an integer, you can use the default comparer, and Advanced Basics 40 msdn magazine
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.