MSDN Magazine - September 2008 - (Page 36) What if you want to use a value that isn’t a simple type like the key in your dictionary? Maybe you want to use the entire Product as the dictionary key. In that case, you must again supply an instance of a custom comparer so that you provide a means of comparing instances of the key. In Figure 2, the code uses the entire Product as the key value for each item in the dictionary; simply supply a custom class that implements IEqualityComparer(Of Product). This class, which is also in the sample project, determines that two products are equal if their ProductID fields are equal. In Figure 3, the first lambda expression calculates the key, the secFigure 3 Comparing Two Keys ' From ToDictionaryDemo in the sample: Dim productsDictionary2 As Dictionary(Of Product, String) = _ someProducts.ToDictionary( _ Function(prod) prod, _ Function(prod) prod.ProductName, _ New ProductComparer()) ' Display information from the new dictionary: sw = New StringWriter() For Each kvp In productsDictionary2 sw.WriteLine("{0}: {1}", kvp.Key.ProductID, kvp.Value) Next ond calculates the value, and the instance of the ProductComparer class provides the means of comparing two keys. Figures 2 and 3 fill the StringWriter instance with the same output, using differently configured dictionaries to create the same results: 1: 2: 24: 34: 35: 38: 39: 43: 67: 70: 75: 76: Chai Chang Guaraná Fantástica Sasquatch Ale Steeleye Stout Côte de Blaye Chartreuse verte Ipoh Coffee Laughing Lumberjack Lager Outback Lager Rhönbräu Klosterbier Lakkalikööri Some methods specifically require a generic List as input, rather than an enumerable sequence. To convert to a List, use the Enumerable.ToList method. The following code retrieves a list of product names, converts them to a generic List(Of String), and then uses the IndexOf method to locate an item in the list: ' From ToListDemo in the sample: Dim db As New SimpleDataContext Dim productNames = _ db.Products.Select(Function(prod) prod.ProductName).ToList() Dim results = _ String.Format("Chang was found at index {0}", _ productNames.IndexOf("Chang")) Figure 4 Convert IEnumerable Sequence into a Lookup ' From ToLookupDemo in the sample: Dim db As New SimpleDataContext Dim products = _ From prod In db.Products _ Where prod.UnitPrice > 40 _ Order By prod.CategoryID Dim lookup = _ products.ToLookup( _ Function(prod) CInt(prod.CategoryID), _ Function(prod) prod) ' Iterate through the lookup: sw = New StringWriter For Each grouping In lookup sw.WriteLine("Category ID = {0}", grouping.Key) For Each value In grouping sw.WriteLine(" {0} ({1:C})", _ value.ProductName, value.UnitPrice) Next Next After running the sample code, the variable “results” contains the following text: Chang was found at index 2 Figure 5 Text for StringWriter Category ID = 1 Côte de Blaye ($263.50) Ipoh Coffee ($46.00) Category ID = 2 Vegie-spread ($43.90) Category ID = 3 Tarte au sucre ($49.30) Sir Rodney's Marmalade ($81.00) Schoggi Schokolade ($43.90) Category ID = 4 Raclette Courdavault ($55.00) Category ID = 6 Thüringer Rostbratwurst ($123.79) Mishi Kobe Niku ($97.00) Category ID = 7 Rössle Sauerkraut ($45.60) Manjimup Dried Apples ($53.00) Category ID = 8 Carnarvon Tigers ($62.50) A Dictionary data structure maps a key to a single value. A Lookup data structure maps a key to a group of values. This structure is a perfect match for a hierarchical Enumerable instance (for example, a CategoryID linking to a number of Product instances). The Enumerable.ToLookup method performs the conversion for you, assuming that you have a simple hierarchy of a key to values. The code in Figure 4 converts an IEnumerable(Of Product) sequence into a Lookup, where each key is a CategoryID, and each value is a Product. The first parameter to the ToLookup method is a function that determines the key, and the second is a function that determines the value for each item. Running the sample code places the text in Figure 5 into the StringWriter variable. Each key is associated with more than one product, and the information is stored in the Lookup instance. Now, let’s say you have a non-generic collection of some sort, but you’d like to apply standard query operators to it. For example, you might have an ArrayList containing data, and you would like to filter the list using a Where method call. The Enumerable.Cast method casts each element of a collection to a specific type and returns a generic IEnumerable instance containing the specified type. You can then operate on the result, as shown in Figure 6. Be aware that Enumerable.Cast throws an exception if it can’t convert all the input elements to the specified type. You can use the OfType method to filter the list before converting it, if that’s the case. Figure 6 filters the ArrayList data to retrieve only those items that begin with the letter “A.” After running the code in Figure 6, the variable named results contains the following items: August April 36 msdn magazine Advanced Basics
Table of Contents Feed for the Digital Edition of MSDN Magazine - September 2008 MSDN Magazine - September 2008 Contents Toolbox CLR Inside Out Data Points Advanced Basics Office Space Cutting Edge Hierarchy ID New Features for Microsoft SQL Server 2008 Prism Data Services Advanced WPF Test Run Security Briefs Foundations { End Bracket } MSDN Magazine - September 2008 MSDN Magazine - September 2008 - (Page Intro) MSDN Magazine - September 2008 - Contents (Page Cover1) MSDN Magazine - September 2008 - Contents (Page Cover2) MSDN Magazine - September 2008 - Contents (Page 1) MSDN Magazine - September 2008 - Contents (Page 2) MSDN Magazine - September 2008 - Contents (Page 3) MSDN Magazine - September 2008 - Contents (Page 4) MSDN Magazine - September 2008 - Contents (Page 5) MSDN Magazine - September 2008 - Contents (Page 6) MSDN Magazine - September 2008 - Contents (Page 7) MSDN Magazine - September 2008 - Contents (Page 8) MSDN Magazine - September 2008 - Contents (Page 9) MSDN Magazine - September 2008 - Contents (Page 10) MSDN Magazine - September 2008 - Toolbox (Page 11) MSDN Magazine - September 2008 - Toolbox (Page 12) MSDN Magazine - September 2008 - Toolbox (Page 13) MSDN Magazine - September 2008 - Toolbox (Page 14) MSDN Magazine - September 2008 - Toolbox (Page 15) MSDN Magazine - September 2008 - Toolbox (Page 16) MSDN Magazine - September 2008 - Toolbox (Page 17) MSDN Magazine - September 2008 - Toolbox (Page 18) MSDN Magazine - September 2008 - CLR Inside Out (Page 19) MSDN Magazine - September 2008 - CLR Inside Out (Page 20) MSDN Magazine - September 2008 - CLR Inside Out (Page 21) MSDN Magazine - September 2008 - CLR Inside Out (Page 22) MSDN Magazine - September 2008 - CLR Inside Out (Page 23) MSDN Magazine - September 2008 - CLR Inside Out (Page 24) MSDN Magazine - September 2008 - CLR Inside Out (Page 25) MSDN Magazine - September 2008 - CLR Inside Out (Page 26) MSDN Magazine - September 2008 - Data Points (Page 27) MSDN Magazine - September 2008 - Data Points (Page 28) MSDN Magazine - September 2008 - Data Points (Page 29) MSDN Magazine - September 2008 - Data Points (Page 30) MSDN Magazine - September 2008 - Data Points (Page 31) MSDN Magazine - September 2008 - Data Points (Page 32) MSDN Magazine - September 2008 - Data Points (Page 33) MSDN Magazine - September 2008 - Data Points (Page 34) MSDN Magazine - September 2008 - Advanced Basics (Page 35) MSDN Magazine - September 2008 - Advanced Basics (Page 36) MSDN Magazine - September 2008 - Advanced Basics (Page 37) MSDN Magazine - September 2008 - Advanced Basics (Page 38) MSDN Magazine - September 2008 - Advanced Basics (Page 39) MSDN Magazine - September 2008 - Advanced Basics (Page 40) MSDN Magazine - September 2008 - Advanced Basics (Page 41) MSDN Magazine - September 2008 - Advanced Basics (Page 42) MSDN Magazine - September 2008 - Advanced Basics (Page 43) MSDN Magazine - September 2008 - Advanced Basics (Page 44) MSDN Magazine - September 2008 - Office Space (Page 45) MSDN Magazine - September 2008 - Office Space (Page 46) MSDN Magazine - September 2008 - Office Space (Page 47) MSDN Magazine - September 2008 - Office Space (Page 48) MSDN Magazine - September 2008 - Office Space (Page 49) MSDN Magazine - September 2008 - Office Space (Page 50) MSDN Magazine - September 2008 - Office Space (Page 51) MSDN Magazine - September 2008 - Office Space (Page 52) MSDN Magazine - September 2008 - Cutting Edge (Page 53) MSDN Magazine - September 2008 - Cutting Edge (Page 54) MSDN Magazine - September 2008 - Cutting Edge (Page 55) MSDN Magazine - September 2008 - Cutting Edge (Page 56) MSDN Magazine - September 2008 - Cutting Edge (Page 57) MSDN Magazine - September 2008 - New Features for Microsoft SQL Server 2008 (Page 58) MSDN Magazine - September 2008 - New Features for Microsoft SQL Server 2008 (Page 59) MSDN Magazine - September 2008 - New Features for Microsoft SQL Server 2008 (Page 60) MSDN Magazine - September 2008 - New Features for Microsoft SQL Server 2008 (Page 61) MSDN Magazine - September 2008 - New Features for Microsoft SQL Server 2008 (Page 62) MSDN Magazine - September 2008 - New Features for Microsoft SQL Server 2008 (Page 63) MSDN Magazine - September 2008 - New Features for Microsoft SQL Server 2008 (Page 64) MSDN Magazine - September 2008 - New Features for Microsoft SQL Server 2008 (Page 65) MSDN Magazine - September 2008 - New Features for Microsoft SQL Server 2008 (Page 66) MSDN Magazine - September 2008 - New Features for Microsoft SQL Server 2008 (Page 67) MSDN Magazine - September 2008 - New Features for Microsoft SQL Server 2008 (Page 68) MSDN Magazine - September 2008 - New Features for Microsoft SQL Server 2008 (Page 69) MSDN Magazine - September 2008 - Prism (Page 70) MSDN Magazine - September 2008 - Prism (Page 71) MSDN Magazine - September 2008 - Prism (Page 72) MSDN Magazine - September 2008 - Prism (Page 73) MSDN Magazine - September 2008 - Prism (Page 74) MSDN Magazine - September 2008 - Prism (Page 75) MSDN Magazine - September 2008 - Prism (Page 76) MSDN Magazine - September 2008 - Prism (Page 77) MSDN Magazine - September 2008 - Prism (Page 78) MSDN Magazine - September 2008 - Prism (Page 79) MSDN Magazine - September 2008 - Data Services (Page 80) MSDN Magazine - September 2008 - Data Services (Page 81) MSDN Magazine - September 2008 - Data Services (Page 82) MSDN Magazine - September 2008 - Data Services (Page 83) MSDN Magazine - September 2008 - Data Services (Page 84) MSDN Magazine - September 2008 - Data Services (Page 85) MSDN Magazine - September 2008 - Data Services (Page 86) MSDN Magazine - September 2008 - Advanced WPF (Page 87) MSDN Magazine - September 2008 - Advanced WPF (Page 88) MSDN Magazine - September 2008 - Advanced WPF (Page 89) MSDN Magazine - September 2008 - Advanced WPF (Page 90) MSDN Magazine - September 2008 - Advanced WPF (Page 91) MSDN Magazine - September 2008 - Advanced WPF (Page 92) MSDN Magazine - September 2008 - Advanced WPF (Page 93) MSDN Magazine - September 2008 - Advanced WPF (Page 94) MSDN Magazine - September 2008 - Advanced WPF (Page 95) MSDN Magazine - September 2008 - Advanced WPF (Page 96) MSDN Magazine - September 2008 - Test Run (Page 97) MSDN Magazine - September 2008 - Test Run (Page 98) MSDN Magazine - September 2008 - Test Run (Page 99) MSDN Magazine - September 2008 - Test Run (Page 100) MSDN Magazine - September 2008 - Test Run (Page 101) MSDN Magazine - September 2008 - Test Run (Page 102) MSDN Magazine - September 2008 - Test Run (Page 103) MSDN Magazine - September 2008 - Test Run (Page 104) MSDN Magazine - September 2008 - Security Briefs (Page 105) MSDN Magazine - September 2008 - Security Briefs (Page 106) MSDN Magazine - September 2008 - Security Briefs (Page 107) MSDN Magazine - September 2008 - Security Briefs (Page 108) MSDN Magazine - September 2008 - Security Briefs (Page 109) MSDN Magazine - September 2008 - Security Briefs (Page 110) MSDN Magazine - September 2008 - Security Briefs (Page 111) MSDN Magazine - September 2008 - Security Briefs (Page 112) MSDN Magazine - September 2008 - Foundations (Page 113) MSDN Magazine - September 2008 - Foundations (Page 114) MSDN Magazine - September 2008 - Foundations (Page 115) MSDN Magazine - September 2008 - Foundations (Page 116) MSDN Magazine - September 2008 - Foundations (Page 117) MSDN Magazine - September 2008 - Foundations (Page 118) MSDN Magazine - September 2008 - Foundations (Page 119) MSDN Magazine - September 2008 - { End Bracket } (Page 120) MSDN Magazine - September 2008 - { End Bracket } (Page Cover3) MSDN Magazine - September 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.