MSDN Magazine - July 2008 - (Page 39) items.Add("Monday") items.Add(3) items.Add(5) items.Add("September") Dim numbers = items.OfType(Of Integer)() Dim strings = items.OfType(Of String)() Figure 3 IEqualityComparer Public Class CustomerCountryComparer Implements IEqualityComparer(Of Customer) Public Function Equals1( _ ByVal x As Customer, ByVal y As Customer) As Boolean _ Implements IEqualityComparer(Of Customer).Equals Return x.Country.Equals(y.Country) End Function Public Function GetHashCode1( _ ByVal obj As Customer) As Integer _ Implements IEqualityComparer(Of Customer).GetHashCode Return obj.Country.GetHashCode End Function End Class After running the code, the two output lists contain the follow ing data: 0, 3, 5 January, Monday, September The Enumerable.Where method allows you to specify a condi tion that filters the input sequence. A second overloaded version provides access to the index of each item in the collection so you can filter based on the index as well. The following example uses both the Where and Select methods, first filtering and then pro jecting the results, retrieving a sequence of files in the C:\Windows folder smaller than 100 bytes: ' From WhereDemo in the sample: Dim files As IEnumerable(Of FileInfo) = _ New DirectoryInfo("C:\Windows").GetFiles() Dim fileResults = _ files _ .Where(Function(file) file.Length < 100) _ .Select(Function(file) _ String.Format("{0} ({1})", file.Name, file.Length)) Figure 4 Using DefaultIfEmpty ' From DefaultIfEmptyDemo in the sample: Dim db As New SimpleDataContext Dim sw As New StringWriter Dim noCustomers = _ From cust In db.Customers Where cust.Country = "XXX" Dim noCustomers1 = _ noCustomers.DefaultIfEmpty() Dim results = _ String.Format( _ "noCustomers contains {0} element(s). " & _ "noCustomers1 contains {1} element(s).", _ noCustomers.Count, noCustomers1.Count) sw.WriteLine(results) ' You can specify the exact value to use if the ' collection is empty: noCustomers1 = noCustomers.DefaultIfEmpty( _ New Customer With {.CustomerID = "XXXXX"}) For Each cust In noCustomers1 sw.WriteLine(cust.CustomerID) Next Because arrays implement the IEnumerable interface, you can use the Where method to filter array contents just as with any other collection. In this case, the code filters the array of FileInfo objects returned by the GetFiles method. Also note that the example cas cades calls to the Where method and then the Select method. On my computer, the sample returned the following results: Addrfixr.ini (62) bthservsdp.dat (12) iltwain.ini (36) S86D5A060.tmp (48) setuperr.log (0) You can use the second overload of the Where method to ac cess the index of each input item that is processed. For example, the following Where method call returns files less than 100 bytes in size that also appear within the first 20 input files: ' From WhereDemo in the sample: fileResults = _ files _ .Where(Function(file, index) _ (file.Length < 100) And (index < 20)) _ .Select(Function(file) _ String.Format("{0} ({1})", file.Name, file.Length)) In this case, on my computer, the results looked like this (the miss ing files fell outside the first 20 files processed): Addrfixr.ini (62) bthservsdp.dat (12) iltwain.ini (36) The Enumerable.Distinct method allows you to filter a list so that the output sequence contains only the unique items from the input list. For simple values (strings, numbers, and so on), this process is easy. For lists of complex objects, how can the runtime engine per form the comparisons to determine whether or not two objects are duplicates? It can’t simply compare the instance variables, as this would, in practice, simply compare the memory addresses of the objects. Instead, for complex objects, you must supply a compar er—an instance of a class that implements IEqualityComparer(Of msdnmagazine.com T) to perform the comparison. (This same issue applies to several methods of the Enumerable class, and it will show up again later in this column.) Suppose you had a sequence containing customer information and you wanted a unique list of countries from where those cus tomers came. If you had a simple list of countries, you could use the default comparer to compare the strings. In this case, you have a list of customers instead. (Yes, you could use the Select method to project the list so that you only had a list of countries, but then you’d lose all the remaining data for other calculations using the list.) The sample project includes the CustomerCountryComparer class, which implements the IEqualityComparer(Of Customer) interface, as shown in Figure 3. The sample code demonstrates two different ways to use the Distinct method. The first projects a list of customers to a list of country names first, and then uses the default string comparer to reduce the list so that it’s unique. The second uses the specific CustomerCountry Comparer class to perform the comparisons and create the unique list, and then projects the resulting distinct list of customers into a list of strings: July 2008 39 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.