MSDN Magazine - October 2007 - (Page 32) Figure 4 Anonymous Types Matched from Query and Variable ‘[Part A] Anonymous Type variable declared Dim NewCustomer = New With {.Name = “Bill Horst”, _ .Age = 25, _ .City = “Seattle”} ‘[Part B] Member recognized by Compiler/IntelliSense Dim NewCustomerCity = NewCustomer.City ‘[Part C] Name, Age and City selected from records in ‘ Customers database table Dim SeattleCustomers = From cust In db.Customers _ Where cust.City = “Seattle” _ Select cust.Name, cust.Age, cust.City ‘[Part D] Seattle Customers stored in a variable with ‘ type List(Of ) Dim SCList = SeattleCustomers.ToList() ‘[Part E] Variable Type and Query Result Type match SCList.Add(NewCustomer) you don’t need to define a type for each combination of members you’d like to use in the Select portion of your query expressions. For this reason, the Visual Basic compiler generates an anonymous type containing the appropriate members and infers that type if an element is referred to later in code. In the following For loop, the type of the variable Ord is inferred as the same anonymous type I discussed for OrdersShippedToWA: For Each Ord In OrdersShippedToWA Dim City As String = Ord.City Next specify a varying number of fields (and in different orders) without having to define objects for every possible variant in advance. Because the object does not necessarily match an already-defined type, the compiler generates a type implicitly (because the name of this type is inconsequential and is not exposed to the developer for use in code, it’s called an anonymous type). Using the type inference functionality, the compiler is able to treat this object of an anonymous type just as it would treat an object of an explicitly defined type. Anonymous types will be most useful to LINQ users who write SQL-style queries in Visual Basic. Here’s an example of how anonymous types can help in your LINQ development. Suppose you have a database that includes records of your company’s orders, and you want to view certain information for all the orders that have shipped to the state of Washington. The following SQL code assumes a table called Orders whose records contain various fields, including ID, Date, Address, City, State, Zip, and Shipped. The query would return all records in the table where the Shipped field has the value True and the State field has the value Washington: SELECT Orders.ID, Orders.Date AS OrderDate, Orders.Address, Orders.City, Orders.State, Orders.Zip FROM Orders WHERE Shipped = ‘True’ AND State = ‘Washington’ In this example, the variable Ord is of a type that has not been explicitly declared, but whose member names are known. The background compiler is able to infer the type, and you get the same IntelliSense support to which you are accustomed. This is the primary reason type inference has been added to Visual Basic 2008. Imagine another scenario where you want to deal with the entire Order object. If only one expression is being selected by the query, the specific type is inferred. If there are methods or other members defined for the type used in the collection, they can be called through type inference, as well: Dim SortedWAOrders = From Ord In Orders _ Select Ord _ Order By Ord.ID Ascending Dim FirstShipTime = SortedWAOrders(0).CalculateShipTime() In fact, thanks to type inference, you could use your methods in the query itself. Consider the following example, in which the results are returned in order of the ship time calculated by a method: Dim SortedShipTimes = From Ord In Orders _ Select Ord _ Order By Ord.CalculateShipTime() Ascending With the development of LINQ, a similar operation can be performed in Visual Basic. The following code performs this same query on Orders and assigns the result to the variable OrdersShippedToWA. Orders could be a database table, an inmemory array or collection, or any type that implements the IEnumerable interface: Dim OrdersShippedToWA = From Ord In Orders _ Where Ord.Shipped _ And Ord.State = “Washington” _ Select Ord.ID, OrderDate = Ord.Date, _ Ord.Address, Ord.City, _ Ord.State, Ord.Zip There may be occasions where it is convenient for you to initialize an anonymous type in code, so this is also supported in Visual Basic. In Figure 4, Part A shows the NewCustomer variable created without an explicit type. Note that the types of the members in the definition are being inferred as well, since they are not defined anywhere. Part B shows another assignment, where a member of the anonymous type is recognized by the compiler. IntelliSense functionality is supported on the variable when it is referred to in the NewCustomerCity assignment. In Part C, a query called SeattleCustomers, which selects members that match the members of the anonymous type, is initialized. In this case, the compiler recognizes that an anonymous type already exists that matches the one needed for this statement. Instead of creating a new anonymous type, the same type is inferred for SeattleCustomers as for NewCustomer. In Part E, the object assigned to NewCustomer can be added to the data from the SeattleCustomers query because type inference recognized the same anonymous type could be used in both places. Type Inference and Lambda Expressions Type inference is also helpful in accommodating Lambda expressions. A Lambda expression is a new Visual Basic language feature that allows the use of delegates for simple functions in an inline fashion. This is particularly useful for passing simple delegates as parameters. Without having to even name a function, a Lambda The type that will be inferred for OrdersShippedToWA is IEnumerable(Of ). Each element in the IEnumerable will then have the members that were selected in the query—ID, OrderDate, Address, City, State, and Zip. As you can probably imagine, LINQ is much easier to use and more agile if 32 msdnmagazine Basic Instincts
Table of Contents Feed for the Digital Edition of MSDN Magazine - October 2007 Cover Contents Toolbox CLR Inside Out Basic Instincts Data Points Cutting Edge Pooled Threads WPF Threads Parallel Linq Parallel Performance Mobile Apps Test Run Foundations Windows with C++ Netting C++ .NET Matters { End Bracket } Net Nuptials MSDN Magazine - October 2007 MSDN Magazine - October 2007 - Contents (Page Cover1) MSDN Magazine - October 2007 - Contents (Page Cover2) MSDN Magazine - October 2007 - Contents (Page 1) MSDN Magazine - October 2007 - Contents (Page 2) MSDN Magazine - October 2007 - Contents (Page 3) MSDN Magazine - October 2007 - Contents (Page 4) MSDN Magazine - October 2007 - Contents (Page 5) MSDN Magazine - October 2007 - Contents (Page 6) MSDN Magazine - October 2007 - Contents (Page 7) MSDN Magazine - October 2007 - Contents (Page 8) MSDN Magazine - October 2007 - Contents (Page 9) MSDN Magazine - October 2007 - Contents (Page 10) MSDN Magazine - October 2007 - Toolbox (Page 11) MSDN Magazine - October 2007 - Toolbox (Page 12) MSDN Magazine - October 2007 - Toolbox (Page 13) MSDN Magazine - October 2007 - Toolbox (Page 14) MSDN Magazine - October 2007 - Toolbox (Page 15) MSDN Magazine - October 2007 - Toolbox (Page 16) MSDN Magazine - October 2007 - CLR Inside Out (Page 17) MSDN Magazine - October 2007 - CLR Inside Out (Page 18) MSDN Magazine - October 2007 - CLR Inside Out (Page 19) MSDN Magazine - October 2007 - CLR Inside Out (Page 20) MSDN Magazine - October 2007 - CLR Inside Out (Page 21) MSDN Magazine - October 2007 - CLR Inside Out (Page 22) MSDN Magazine - October 2007 - CLR Inside Out (Page 23) MSDN Magazine - October 2007 - CLR Inside Out (Page 24) MSDN Magazine - October 2007 - CLR Inside Out (Page 25) MSDN Magazine - October 2007 - CLR Inside Out (Page 26) MSDN Magazine - October 2007 - CLR Inside Out (Page 27) MSDN Magazine - October 2007 - CLR Inside Out (Page 28) MSDN Magazine - October 2007 - CLR Inside Out (Page 29) MSDN Magazine - October 2007 - CLR Inside Out (Page 30) MSDN Magazine - October 2007 - Basic Instincts (Page 31) MSDN Magazine - October 2007 - Basic Instincts (Page 32) MSDN Magazine - October 2007 - Basic Instincts (Page 33) MSDN Magazine - October 2007 - Basic Instincts (Page 34) MSDN Magazine - October 2007 - Basic Instincts (Page 35) MSDN Magazine - October 2007 - Basic Instincts (Page 36) MSDN Magazine - October 2007 - Data Points (Page 37) MSDN Magazine - October 2007 - Data Points (Page 38) MSDN Magazine - October 2007 - Data Points (Page 39) MSDN Magazine - October 2007 - Data Points (Page 40) MSDN Magazine - October 2007 - Data Points (Page 41) MSDN Magazine - October 2007 - Data Points (Page 42) MSDN Magazine - October 2007 - Cutting Edge (Page 43) MSDN Magazine - October 2007 - Cutting Edge (Page 44) MSDN Magazine - October 2007 - Cutting Edge (Page 45) MSDN Magazine - October 2007 - Cutting Edge (Page 46) MSDN Magazine - October 2007 - Cutting Edge (Page 47) MSDN Magazine - October 2007 - Cutting Edge (Page 48) MSDN Magazine - October 2007 - Cutting Edge (Page 49) MSDN Magazine - October 2007 - Cutting Edge (Page 50) MSDN Magazine - October 2007 - Cutting Edge (Page 51) MSDN Magazine - October 2007 - Cutting Edge (Page 52) MSDN Magazine - October 2007 - Cutting Edge (Page 53) MSDN Magazine - October 2007 - Pooled Threads (Page 54) MSDN Magazine - October 2007 - Pooled Threads (Page 55) MSDN Magazine - October 2007 - Pooled Threads (Page 56) MSDN Magazine - October 2007 - Pooled Threads (Page 57) MSDN Magazine - October 2007 - Pooled Threads (Page 58) MSDN Magazine - October 2007 - Pooled Threads (Page 59) MSDN Magazine - October 2007 - Pooled Threads (Page 60) MSDN Magazine - October 2007 - Pooled Threads (Page 61) MSDN Magazine - October 2007 - Pooled Threads (Page 62) MSDN Magazine - October 2007 - Pooled Threads (Page 63) MSDN Magazine - October 2007 - Pooled Threads (Page 64) MSDN Magazine - October 2007 - Pooled Threads (Page 65) MSDN Magazine - October 2007 - WPF Threads (Page 66) MSDN Magazine - October 2007 - WPF Threads (Page 67) MSDN Magazine - October 2007 - WPF Threads (Page 68) MSDN Magazine - October 2007 - WPF Threads (Page 69) MSDN Magazine - October 2007 - Parallel Linq (Page 70) MSDN Magazine - October 2007 - Parallel Linq (Page 71) MSDN Magazine - October 2007 - Parallel Linq (Page 72) MSDN Magazine - October 2007 - Parallel Linq (Page 73) MSDN Magazine - October 2007 - Parallel Linq (Page 74) MSDN Magazine - October 2007 - Parallel Linq (Page 75) MSDN Magazine - October 2007 - Parallel Linq (Page 76) MSDN Magazine - October 2007 - Parallel Linq (Page 77) MSDN Magazine - October 2007 - Parallel Linq (Page 78) MSDN Magazine - October 2007 - Parallel Performance (Page 79) MSDN Magazine - October 2007 - Parallel Performance (Page 80) MSDN Magazine - October 2007 - Parallel Performance (Page 81) MSDN Magazine - October 2007 - Parallel Performance (Page 82) MSDN Magazine - October 2007 - Parallel Performance (Page 83) MSDN Magazine - October 2007 - Parallel Performance (Page 84) MSDN Magazine - October 2007 - Parallel Performance (Page 85) MSDN Magazine - October 2007 - Parallel Performance (Page 86) MSDN Magazine - October 2007 - Parallel Performance (Page 87) MSDN Magazine - October 2007 - Parallel Performance (Page 88) MSDN Magazine - October 2007 - Parallel Performance (Page 89) MSDN Magazine - October 2007 - Parallel Performance (Page 90) MSDN Magazine - October 2007 - Mobile Apps (Page 91) MSDN Magazine - October 2007 - Mobile Apps (Page 92) MSDN Magazine - October 2007 - Mobile Apps (Page 93) MSDN Magazine - October 2007 - Mobile Apps (Page 94) MSDN Magazine - October 2007 - Mobile Apps (Page 95) MSDN Magazine - October 2007 - Mobile Apps (Page 96) MSDN Magazine - October 2007 - Mobile Apps (Page 97) MSDN Magazine - October 2007 - Mobile Apps (Page 98) MSDN Magazine - October 2007 - Mobile Apps (Page 99) MSDN Magazine - October 2007 - Mobile Apps (Page 100) MSDN Magazine - October 2007 - Test Run (Page 101) MSDN Magazine - October 2007 - Test Run (Page 102) MSDN Magazine - October 2007 - Test Run (Page 103) MSDN Magazine - October 2007 - Test Run (Page 104) MSDN Magazine - October 2007 - Test Run (Page 105) MSDN Magazine - October 2007 - Test Run (Page 106) MSDN Magazine - October 2007 - Test Run (Page 107) MSDN Magazine - October 2007 - Test Run (Page 108) MSDN Magazine - October 2007 - Test Run (Page 109) MSDN Magazine - October 2007 - Test Run (Page 110) MSDN Magazine - October 2007 - Test Run (Page 111) MSDN Magazine - October 2007 - Test Run (Page 112) MSDN Magazine - October 2007 - Test Run (Page 113) MSDN Magazine - October 2007 - Test Run (Page 114) MSDN Magazine - October 2007 - Foundations (Page 115) MSDN Magazine - October 2007 - Foundations (Page 116) MSDN Magazine - October 2007 - Foundations (Page 117) MSDN Magazine - October 2007 - Foundations (Page 118) MSDN Magazine - October 2007 - Foundations (Page 119) MSDN Magazine - October 2007 - Foundations (Page 120) MSDN Magazine - October 2007 - Foundations (Page 121) MSDN Magazine - October 2007 - Foundations (Page 122) MSDN Magazine - October 2007 - Foundations (Page 123) MSDN Magazine - October 2007 - Foundations (Page 124) MSDN Magazine - October 2007 - Windows with C++ (Page 125) MSDN Magazine - October 2007 - Windows with C++ (Page 126) MSDN Magazine - October 2007 - Windows with C++ (Page 127) MSDN Magazine - October 2007 - Windows with C++ (Page 128) MSDN Magazine - October 2007 - Windows with C++ (Page 129) MSDN Magazine - October 2007 - Windows with C++ (Page 130) MSDN Magazine - October 2007 - Windows with C++ (Page 131) MSDN Magazine - October 2007 - Windows with C++ (Page 132) MSDN Magazine - October 2007 - Netting C++ (Page 133) MSDN Magazine - October 2007 - Netting C++ (Page 134) MSDN Magazine - October 2007 - Netting C++ (Page 135) MSDN Magazine - October 2007 - Netting C++ (Page 136) MSDN Magazine - October 2007 - .NET Matters (Page 137) MSDN Magazine - October 2007 - .NET Matters (Page 138) MSDN Magazine - October 2007 - .NET Matters (Page 139) MSDN Magazine - October 2007 - .NET Matters (Page 140) MSDN Magazine - October 2007 - .NET Matters (Page 141) MSDN Magazine - October 2007 - .NET Matters (Page 142) MSDN Magazine - October 2007 - .NET Matters (Page 143) MSDN Magazine - October 2007 - Net Nuptials (Page 144) MSDN Magazine - October 2007 - Net Nuptials (Page Cover3) MSDN Magazine - October 2007 - Net Nuptials (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.